java - 在 Android 10 上阅读 PDF
问题描述
需要在 Android 10 上阅读 PDF 以下代码在 Android 9.0 上运行良好,但在 Android 10 上,负责显示 PDF 的应用程序只是闪烁并返回到我的应用程序。
File pdfFile = new File(getExternalFilesDir(null), "Pdf");
pdfFile.setReadable(true);
if (pdfFile.exists()) {
if (pdfFile.canRead()) {
if (pdfFile.canWrite()) {
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(objIntent, req_code);
}else{
Toast.makeText(MainActivity.this, "Not writable! ", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "Not readable! ", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "The file does not exists! ", Toast.LENGTH_SHORT).show();
}
在 onActivityResult 方法上,我收到 RESULT_CANCELED 作为 result_code 和 data == null。
解决方案
以下代码在 Android 9.0 上运行良好
您项目中的某个人做了一些不幸的事情,让该代码在 Android 9 上运行。它应该在三年前失败,从 Android 7.0 开始,使用FileUriExposedException
.
在 Android 10 上,应用程序无法访问外部存储,除了一些有限的应用程序特定位置。这就是 Google 对从 Android 7.0 开始发出的警告,你不应该再使用Uri.fromFile()
了。
三年前的正确解决方案 - 以及今天的正确解决方案 -用于FileProvider
使 PDF 查看器应用程序可以使用该 PDF。
也可以看看:
推荐阅读
- excel - 复制并粘贴到下一个可用行中,忽略格式
- python - 在odoo的act_window xml中调用或添加函数
- java - 使用流 API 将字符串设置为小写但首字母大写
- sql - 如何删除未能通过 sqlite 约束的行?
- javascript - Firebase hitcounter:命中显示但不会增加
- android - 如何在命令行上使用 lldb 调试 Android 上的 C++ 代码
- php - 如何从 Symfony3 中提交的表单中获取数据?
- sql-server - 有没有办法将 .sqlproj 添加到 SSMS 解决方案资源管理器?
- reactjs - 为什么这个 JSX 中有一个随机空间?
- mysql - 如何登录 Mariadb?