Search Unity

Download and open PDF

Discussion in 'Android' started by Cascho01, Mar 8, 2019.

  1. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Hi,

    I want to download a PDF-file (or any other file, e.g. *.jpg) in my Android-App and afterwards open it.
    Seems to be very complicated since API 23 (Thread, solution doesn´t work for me)

    No matter if I need write permissions / sdcard etc, it would be great to have a working example.

    Please!

    Thank you very much!
     
    Last edited: Mar 9, 2019
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    When you mean open it, you mean launch in Adobe Reader or similar, on the device?
     
  3. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
  4. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    I want to open the downloaded file from inside my Android Unity-App with a button and a method like Application.OpenUrl().
     
  5. MiClr

    MiClr

    Joined:
    Jul 11, 2018
    Posts:
    2
    Hello Cascho01!
    Did you find how open it?
     
  6. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
  7. MiClr

    MiClr

    Joined:
    Jul 11, 2018
    Posts:
    2
    Thanks for your answer!
    Good luck to you.
     
  8. alevolhard

    alevolhard

    Joined:
    Oct 25, 2018
    Posts:
    3
    Not yet?
     
  9. blamejane

    blamejane

    Joined:
    Jul 8, 2013
    Posts:
    233
    Anyone get this working? I'm trying to navigate to a PDF located on a website. Like: https://www.google.com/some.pdf

    but my app crashes when I call Application.OpenUrl(). This is not a local pdf, so FileBrowswer or whatever doesn't apply.

    Am I missing a permission?
     
  10. roadrage

    roadrage

    Joined:
    Jul 24, 2018
    Posts:
    2
    I opened chrome in kiosk mode using command line interface.

    string chromePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
    string arguments = url+" --kiosk";
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(chromePath,arguments);
    System.Diagnostics.Process.Start(startInfo);

    Using similar way we can open adobe reader.
    string adobePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\AcroRD32.exe";
    string arguments = url+" <Path of pdf>";
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(chromePath,arguments);
    System.Diagnostics.Process.Start(startInfo);
    // We can add other arguments to specify our query -->
    //arguments += "other arguments";
    //http://www.robvanderwoude.com/commandlineswitches.php#Acrobat
     
  11. starlprd

    starlprd

    Joined:
    Sep 20, 2018
    Posts:
    3
    I was looking for same but couldnt find many leads below is my working code to download pdf from given url and store in android path. this code can process any file and can be saved, That file just need to put in exact file format in the name(ex. xyz.pdf or abc.docx). so i used exact name given in url while downloading.
    Hope It helps.

    Code (CSharp):
    1. IEnumerator SavePdf()
    2.     {
    3.         UnityWebRequest request = UnityWebRequest.Get("   Your pdf Url Path here   ");
    4.         yield return request.SendWebRequest();
    5.  
    6.         if (request.isNetworkError || request.isHttpError)
    7.         {
    8.             Debug.Log(request.error);
    9.         }
    10.         else
    11.         {
    12.        
    13.             string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android",System.StringComparison.Ordinal));   //pdf saving path
    14.             string savePath =  path + "/MyDocs/";
    15.             if(!Directory.Exists(savePath))
    16.             {
    17.                 Directory.CreateDirectory(savePath);
    18.             }      
    19.             string[] temp = pdfUrl.Split('/');
    20.             string x = Path.Combine(savePath,temp[temp.Length-1]);
    21.             System.IO.File.WriteAllBytes (x, request.downloadHandler.data);
    22.                  
    23.  
    24.         }
    25.     }
     
    Last edited: Dec 9, 2021
  12. LeonXoneZ

    LeonXoneZ

    Joined:
    Nov 8, 2021
    Posts:
    1
    Hi thanks for sharing your code, on the 19th line you put pdfUrl.Split('/') thats a plugin or a library ? or it is native feature?
     
  13. starlprd

    starlprd

    Joined:
    Sep 20, 2018
    Posts:
    3
    its a native feature, its basically a String.split which splits data by character eg. if i have file path "android/data/document/abcd.pdf" then that will split it into four different strings to give you array with length 4 where arr[0]=android, arr[1]=data, arr[2]=document, arr[3]=abcd.pdf the last arr[3] is been used so we could save the file with same exact name as it was saved on server
     
    Last edited: Dec 23, 2021
  14. teli-unity

    teli-unity

    Joined:
    Apr 6, 2017
    Posts:
    10
    I wrote some library functions for downloading Text(JSON), Audio, or PDF here with the prefix "Web"
     
    khanhabib likes this.