我想使用 Intent 打开任何文件(如果可能)。我有我的 openFile(Uri file, String mimeType)
方法已定义,并使用已注册的 BroadcastReceiver 调用它。方法如下:
private void openFile(Uri file, String mimeType) {
Intent openFile = new Intent(Intent.ACTION_VIEW);
openFile.setData(file);
try {
context.startActivity(openFile);
} catch (ActivityNotFoundException e) {
Log.i(TAG, "Cannot open file.");
}
}
我目前没有使用 mimeType
,我只是想让它工作。在 Uri 为 content://downloads/all_downloads/3980
的 .pdf 上调用该 openFile 方法的结果只是在 pdf 查看器中打开的空白 pdf(可能正确解释了 mime 类型),文件名显示为 3980 downloadID。
我知道发生了什么,因为无论出于何种原因,内容 Uri 都没有被正确解析。我通过 Uri localUri = downloadManager.getUriForDownloadedFile(downloadId);
行获得了 Uri其中 downloadId
是从 downloadManager.enqueue(request);
返回的 long
当我有内容类型 Uri 时如何打开文件?
最佳答案
调用 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
。否则,其他应用将无权使用该 Uri
标识的内容。
请注意,这仅在您有权使用该Uri
标识的内容时有效。
关于java - 使用 Intent 打开文件 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940181/