Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help opening StreamingAssets PDF on Android

Discussion in 'Android' started by NickR2600, Oct 10, 2019.

  1. NickR2600

    NickR2600

    Joined:
    Aug 17, 2018
    Posts:
    20
    Hello,

    In the previous version of my Android app, I'm able to open PDFs that are stored on a website. Users click a button, and the PDF is retrieved. I'd strongly prefer to bundle the PDFs with the app, so users can open the files without being online. In the current version of my app, I've saved copies of the PDFs in the StreamingAssets folder. According to this documentation, I need to use UnityWebRequest to access these streaming asset files directly on Android. I can't figure out how this is supposed to work, and the examples I've found haven't been too useful. Would anyone have a solution or guidance?

    My code looks something like this. It works on desktop, but not on Android. The user presses a button in the app, which calls tryCoroutine:

    Code (CSharp):
    1.  
    2.     public void tryCoroutine(string filePath){
    3.         StartCoroutine(openFileOnAndroid(filePath));
    4.     }
    5.  
    6.     private IEnumerator openFileOnAndroid(string filePath) {
    7.         UnityWebRequest www = UnityWebRequest.Get(filePath);
    8.         yield return www.SendWebRequest();
    9.         Application.OpenURL(www.url);
    10.     }
    The string filePath would be Path.Combine(Application.streamingAssetsPath, fileName);

    --Thanks
     
  2. NickR2600

    NickR2600

    Joined:
    Aug 17, 2018
    Posts:
    20
    Nevermind. I mostly figured it out. I had to save the bytes as a file locally via downloadHandler, then open it. I don't know if it's possible to open the bytes as some temp file without actually saving anything to disk. This thread on stackoverflow was also helpful: https://stackoverflow.com/questions/50689859/download-large-file
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    The file path for StreamingAssets on Android is an URI that points to a file inside apk. You can try, maybe the pdf viewer can open that URI. If not, then yes, you have to copy file to disk.