gpt4 book ai didi

android - 使用 Android Intent 打开 PDF 文件

转载 作者:行者123 更新时间:2023-11-29 00:59:43 29 4
gpt4 key购买 nike

所以这是场景:

我正在从网络下载 PDF 并将其保存在 /0/Download/NSIT - Notices/"fileName".pdf

现在像这样发送 Intent:

private void openPDF(String fileName) {
Log.d(TAG, "openPDF: called");
Intent intent = new Intent(Intent.ACTION_VIEW);File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
File myPDF = new File(Environment.getExternalStorageDirectory() + "/Download/NSIT - Notices", fileName + ".pdf");
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", myPDF);
Log.d(TAG, "openPDF: intent with uri: " + uri);
intent.setDataAndType(uri, "application/pdf");
context.startActivity(Intent.createChooser(intent, "Open with..."));
}

现在任何 PDF 阅读器(Google Drive PDF 阅读器、系统 PDF 查看器)都在说

The File Does Not Exist

图片: enter image description here

谷歌的那个坐着什么都不做(这就是为什么不包括它的图像)

Mainfest.xml

<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_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

我在 AsyncTask 中创建文件,但从该 AsyncTask 的 postExecute 打开

File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
File myPDF = new File(myDir, params[1] + ".pdf");
if (!myDir.exists())
myDir.mkdirs();
try {
fileOutputStream = new FileOutputStream(myPDF);
fileOutputStream.write(response.bodyAsBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "doInBackground: Download complete");

日志

enter image description here

之前它抛出 FileUriExposedException 然后我修复了这个 FileProvider 问题。问题是我的应用程序下载的 pdf 文件已成功创建,我可以从任何文件管理器(现在是 ES 文件资源管理器)成功打开它,但不能从我的应用程序:(。我是 FileProvider. 需要帮助!

PS:这也不是我的设备问题。测试了另外两部具有不同 ROM 和 API >= 23

的手机

最佳答案

请设置intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

它会将您的文件的访问权限授予其他提供者

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

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