我有一个适用于Android 4,5和6的应用程序。该应用程序列出服务器中的远程文件,并允许您从服务器下载它们(PDF文件)。一旦你点击一个文件,它将它下载到“Android Downloads文件夹”上,当它完成时,它会用默认的PDF阅读器打开它。Android 7 - 无法显示PDF(pdf_name格式无效)
问题是,Android 7中的相同代码未能给出下一个错误:“无法显示PDF(pdf_name是无效格式)”。
这很有趣,因为:
- 如果我访问下载文件夹,点击下载的文件,它会打开正确。
- 下载完成后,会显示一条通知,点击它即可正常打开它。
所以,只有当相同的应用程序尝试下载它后自动打开文件时,才会出现问题。
我怎么办下载:
fileName="file.pdf";
request = new DownloadManager.Request(Uri.parse(Constants.GetURL()));
request.setMimeType(mime)
.setTitle(fileName).setVisibleInDownloadsUi(true)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
.setDescription(mContext.getString(R.string.app_name))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
enqueue = dm.enqueue(request);
一旦下载:
Uri path = Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
//path contains the route where file was downloaded. Something like: file:///storage/emulated/0/Download/file.pdf
Intent pdfOpenintent = IntentHelper.getViewPDFIntent(path);
mContext.startActivity(pdfOpenintent);
我怎么打开它:
public static Intent getViewPDFIntent(Uri path){
Intent i = new Intent(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pdfOpenintent.setDataAndType(path, "application/pdf");
return i;
}
关于FileProvider: 我一直在使用实施FileProvider和问题是t他一样。它与权限无关,下载文件夹是公用文件夹,并且没有SecurityException引发或任何其他与权限相关的异常。
它与任何PDF应用程序无关,因为它发生与Gmail共享发送文档的相同内容。
Extrange行为:当我一步一步地调试应用程序,它完美的作品。当我执行正常时,它失败。如果我睡觉或延迟5秒,不起作用。所以只能调试。
关于安卓7:阅读Android 7的正式更改(https://developer.android.com/about/versions/nougat/android-7.0-changes.html)它说它应该工作,但不要再推荐这种方式。否则,它不起作用。
有没有新的想法?
您试图使用哪种PDF查看器?你认为什么是“路径”的价值?什么是'getViewPDFIntent()'? – CommonsWare
嗨@CommonsWare我修改了回答你的问题的代码。谢谢。 – Christian
一旦你将'targetSdkVersion'提升到25或更高,''''Uri'就不能在Android 7.0+上运行,所以你可能希望已经开始移动到'FileProvider'。而且,正如我以前所问,您试图使用哪种PDF查看器? – CommonsWare