意图打开文件(Android) [英] Open File With Intent (Android)

查看:222
本文介绍了意图打开文件(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个Intent打开任何文件(如果可能).我定义了openFile(Uri file, String mimeType)方法,并使用已注册的BroadcastReceiver调用了该方法.方法如下:

I'd like to open any file (if possible) using an Intent. I have my openFile(Uri file, String mimeType) method defined and it's called using a registered BroadcastReceiver. Here is the method:

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.

I'm not currently using mimeType, I'm just trying to get this to work. The result of that openFile method when called on a .pdf that has a Uri of content://downloads/all_downloads/3980 is just a blank pdf opened in the pdf viewer (mime type is probably interpreted correctly) with the 3980 downloadID shown as the filename.

我知道这是怎么回事,因为无论什么原因,Uri内容都无法正确解析.我得到的是行Uri localUri = downloadManager.getUriForDownloadedFile(downloadId);的Uri,其中downloadIddownloadManager.enqueue(request);

I know what's happening, in that the content Uri is not being resolved properly for whatever reason. I get the Uri with the line Uri localUri = downloadManager.getUriForDownloadedFile(downloadId); where downloadId is the long returned from downloadManager.enqueue(request);

内容类型为Uri时如何打开文件?

How do I open a file when I have a content type Uri?

推荐答案

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

Call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION). Otherwise, the other app will not have rights to work with the content identified by that Uri.

请注意,这仅在 you 有权使用该Uri标识的内容时有效.

Note that this only works if you have rights to work with the content identified by that Uri.

这篇关于意图打开文件(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆