【发布时间】:2018-05-25 18:48:52
【问题描述】:
我正在尝试使用 FileProvider 通过隐式意图打开下载的 pdf 文件。
我正在使用 DownloadManager 从远程服务器下载 pdf 文件,它工作正常。哪个商店在它的目的地。
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadURL));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(mFilename);
request.setDescription("Downloading...");
request.setVisibleInDownloadsUi(true);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);
下载完成后我想打开它。
public void OpenPdfFile(){
File sharedFile = new File(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID+ ".provider", sharedFile);
intent.setDataAndType(uri, "application/pdf");
PackageManager pm = mContext.getPackageManager();
if (intent.resolveActivity(pm) != null) {
mContext.startActivity(intent);
}
}
在清单文件中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
和 provider_paths.xml 一样
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external-path" path="." />
</paths>
我遇到了这个错误
java.lang.IllegalArgumentException:未能找到包含 /Download/FOLDER_NAME/demo_presentationfile.PDF 的已配置根目录
有什么建议吗?
【问题讨论】:
-
复制错误消息 -> 粘贴到 Google -> 注意 first 结果是关于 SO -> sigh -> downvote 的另一个问题。我什至不会添加欺骗的链接。
-
@2Dee 错误的结论。请撤消投票。
标签: android android-intent android-fileprovider