【问题标题】:Open File With Intent (Android)有意打开文件 (Android)
【发布时间】: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


    【解决方案1】:

    致电addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)。否则,其他应用将无权使用该Uri 标识的内容。

    请注意,这仅在有权使用Uri 标识的内容时才有效。

    【讨论】:

    • 正确回答这个问题的速度令人沮丧。我会尽快将其标记为答案。
    • @UnicornMaster:对不起!我会...输入...更多...慢慢...下一次... :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2023-03-07
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多