【发布时间】:2017-03-21 23:37:18
【问题描述】:
我想使用 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); 返回的长字符串
当我有内容类型 Uri 时如何打开文件?
【问题讨论】:
标签: java android android-intent