【发布时间】:2023-03-21 07:04:01
【问题描述】:
我正在尝试从我的 Android 应用程序外部呈现 PDF。 我的代码是这样的
val file = File(<path-to-pdf>)
val target = Intent(Intent.ACTION_VIEW)
target.setDataAndType(Uri.fromFile(file), "application/pdf")
target.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
val intent = Intent.createChooser(target, "Open With")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
try {
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Log.e(e.message)
}
只要在某些设备上运行(目前在 Pixel 2 上观察到),外部阅读器就会启动并立即关闭。 建议的解决方案 here 不起作用。
【问题讨论】:
-
其他应用无法访问您应用中的文件:uris。该限制是在 Android 7.0 中添加的。考虑例如一个 FileProvider 代替。 developer.android.com/training/secure-file-sharing/index.html
-
它确实适用于某些手机。此类问题是否可能仅出现在某些设备上?