简体   繁体   中英

In Unity, how can I open a file (i.e. PDF) in an external app?

I'm working on a Unity app right now, and the client wants to be able to allow the user to click a button and have a PDF open on the phone (iOS and Android), but in whatever app they use for reading PDFs and not inside of the Unity app.

I've tried Application.OpenURL("file:///[...]") , and that works in the editor on my Windows desktop (opens PDF in Edge)... but it doesn't open the file on Android (nothing happens to open the file).

Here's what I'm doing:

public string FileName = string.Empty;
public string FileExtension = string.Empty;
private string FilePath = string.Empty;
void Start()
{
    FilePath = string.Format("{0}/{1}.{2}", Application.persistentDataPath, FileName, FileExtension);
}

public void OpenFile()
{
    if (!File.Exists(FilePath))
    {
        var pathRoot = Application.streamingAssetsPath;
        #if UNITY_ANDROID && !UNITY_EDITOR
            pathRoot = Application.dataPath + "!/assets";
        #endif
        StartCoroutine(FetchFile(string.Format("{0}/{1}.{2}", pathRoot, FileName, FileExtension)));
        return;
    }
    Application.OpenURL(FilePath);
}

private IEnumerator FetchFile(string path)
{
    path = "file://" + path;
    #if UNITY_ANDROID && !UNITY_EDITOR
        path = "jar:" +  path;
    #endif
    var fetcher = new WWW(path);
    yield return fetcher;
    File.WriteAllBytes(FilePath, fetcher.bytes);

    Application.OpenURL("file:///" + FilePath);
}

So I'm checking if the file exists on the device in storage, and if not I am 'extracting' it from the app and writing it to storage. Then I try to open the local 'URL' with the file:/// standard (this Application.OpenURL("https://www.google.com"); does work successfully to open that URL in my mobile browser).

Is there a way in Unity to create an Intent or something to trigger this? The FilePath works on Windows, and is the same path I am writing the bytes to, so that should be correct... or am I missing something?

NOTE: this is also not working in iOS, but I thought I read in the Unity forums (can't find the link now) to Android being the only exception to the paths/URL thing... can anyone help me out with that, too?

Android permissions changed in Nougat (v.7)

you won't be able to open by path only, the sistem was blocking it.

Just lower the target API level to 23 (Android 6.0 'Marshmallow').

you could just use this code as per this reference URL: How to Open a PDF in an Android-Unity3D App?

void openPDF(){
  string namePDF = "test";
  TextAsset pdfTem = Resources.Load("PDFs/"+namePDF, typeof(TextAsset)) as TextAsset;
  System.IO.File.WriteAllBytes(Application.persistentDataPath + "/"+namePDF+".pdf", pdfTem.bytes);
  Application.OpenURL(Application.persistentDataPath+"/"+namePDF+".pdf");
    }

If you are trying to open any document using Application.OpenURL then it won't work. Unity removed that support in latest versions. You can use AndroidOpenUrl.OpenUrl(documentUrl, dataType) to resolve this issue.

public void Example()
 {   
     string dataType = "application/pdf";
     string documentUrl = "/storage/emulated/0/Downloads/template.pdf";
     AndroidOpenUrl.OpenUrl(documentUrl, dataType); // you can specify any MIME type when opening a file by explicitly specifying the dataType parameter
 }

You will get a demo and installation steps of the package from the following repository.

https://github.com/codemaker2015/Unity-Android-Files-Opener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM