我目前正在使用Unity应用,客户端希望能够允许用户单击按钮并在手机(iOS和Android)上打开PDF,但无论使用哪种应用而不是在Unity应用程序内部读取PDF。
我已经尝试过Application.OpenURL("file:///[...]")
,并且可以在Windows桌面上的编辑器中使用(在Edge中打开PDF)...但是在Android上无法打开文件(打开文件没有任何反应)
这就是我在做什么:
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);
}
因此,我正在检查文件在存储设备中是否存在,如果不存在,我正在从应用程序“提取”文件并将其写入存储。然后,我尝试使用file:///
标准打开本地“ URL”(此Application.OpenURL("https://www.google.com");
确实可以成功在我的移动浏览器中打开该URL)。
Unity中是否有创建Intent
的方法或触发它的方法? FilePath
在Windows上可以使用,并且与我将字节写入其中的路径相同,因此应该是正确的……还是我丢失了某些内容?
注意: ,这在iOS中也不起作用,但我想我在Unity论坛(目前找不到链接)中读到,Android是唯一的路径/ URL的异常...任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
在Nougat(v.7)中更改了Android权限
您将无法仅按路径打开,因为系统阻止了它。
只需将目标API级别降低至23(Android 6.0“棉花糖”)。
您可以根据以下参考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");
}