gpt4 book ai didi

android - 打开下载的 PDF 文件

转载 作者:行者123 更新时间:2023-11-29 23:54:30 25 4
gpt4 key购买 nike

我正在尝试使用 FileProvider 通过隐式 Intent 打开下载的 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);
}
}

在 list 文件中

<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 的已配置根目录

有什么建议吗?

最佳答案

File sharedFile = new File(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);

计算出一个不可能的文件系统路径。如果您检查 sharedFile.getAbsolutePath() 的值,您会看到它。

更改为:

File sharedFile = new File(Environment.getExternalStoragePublicDirectory( 
Environment.DIRECTORY_DOWNLOADS), "FOLDER_NAME/" + mFilename);

关于android - 打开下载的 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50526479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com