gpt4 book ai didi

android - 在下载文件夹中打开文件 Android N(API 24+)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:36 24 4
gpt4 key购买 nike

我只想在我的外部下载目录中打开一个 pdf 文件。

我用这段代码打开了文件,它曾经工作正常。

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
+ File.separator + filename);
Uri path = Uri.fromFile(file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setDataAndType(path, "application/" + getExtension(filename));
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
CourseModulesActivity.this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
pdfOpenintent.setType("application/*");
startActivity(Intent.createChooser(pdfOpenintent, "No Application found to open File - " + filename));
}

现在在 android N(API 24+)中,它崩溃并提示 android.os.FileUriExposedException

我点击了链接 - https://stackoverflow.com/a/38858040/4788557https://developer.android.com/training/secure-file-sharing/share-file.html#ShareFile将我的代码转换成这个 -

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
+ File.separator + filename);
Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setData(path);
pdfOpenintent.setType("application/" + getExtension(filename));
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
CourseModulesActivity.this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
pdfOpenintent.setType("application/*");
startActivity(Intent.createChooser(pdfOpenintent, "No Application found to open File - " + filename));
}

并将提供者 xml 添加为 -

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

并表现为 -

<manifest...>
....
<application...>
....
<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>
....
</application>

所以,现在应用程序不会在 N 上崩溃,但是 pdf 应用程序无法打开文件。
这方面有什么帮助吗?

最佳答案

I used this code to open the file, and it used to work fine.

该代码中至少有两个错误:

  • "application/" + getExtension(filename)不是查找文件 MIME 类型的合适算法,因为并非所有 MIME 类型都以 application/ 开头(例如,image/jpegtext/plain)。使用 MimeTypeMap .

  • setType() clears the data previously set on the Intent , 所以你的 Intent没有Uri .

另外,不要使用串联来构建文件路径,因为您的代码无法处理有人决定使用 DIRECTORY_DOWNLOADS 的情况。以 / 结尾.替换:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
+ File.separator + filename);

与:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fiename);

but the pdf applications are unable to open the file

没有Uri在你的Intent ,因为您的第二个代码 fragment 包含与第一个代码 fragment 相同的错误。使用 setDataAndType()设置 Uri和 MIME 类型一次。或者,只需使用 setData()并摆脱 setType() , 作为 FileProvider将使用 MimeTypeMap确定 MIME 类型。

FWIW,this sample app演示使用 FileProvider为 PDF 查看器提供 PDF 文件。

关于android - 在下载文件夹中打开文件 Android N(API 24+),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41707436/

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