从内容 URI 获取搜索图像的绝对文件路径 [英] Getting the Absolute File Path from Content URI for searched images

查看:57
本文介绍了从内容 URI 获取搜索图像的绝对文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用隐式意图将图像从其他应用程序共享到我的应用程序 ACTION_SEND.

当从 chrome 浏览器共享搜索图像时,应用程序接收具有内容 URI 的意图,如下所示:content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg

如何从这种类型的内容 URI 中获取文件路径? Facebook、Google+ 等所有其他应用程序都在这样做.我正在使用 FileChooser 来获取其他类型的内容 URI(例如来自 Gallery)的文件路径.>

尝试到处寻找,没有太多帮助.有人可以建议如何使用这些内容 URI 吗?

解决方案

如果您绝对需要文件的本地副本,您将需要打开 InputStream 将内容复制到本地您知道路径的文件,然后从那里开始.旁注:Guava 的 ByteStreams#copy 是实现此目的的简单方法.

当然这个文件不再受原始 Uri 源的支持,所以我认为这不是你想要的.相反,您应该使用 Uri 的预期 API.查看存储访问框架

编辑

以下是如何从 Uri

获取 InputStream

InputStream inputStream = getContentResolver().openInputStream(uri);

I am trying to share images from other applications to my application using implicit intent ACTION_SEND.

While sharing search images from chrome browser, app receives intent with a Content URI like this: content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg

How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it. I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).

Tried looking everywhere, without much help. Can someone suggest how to work with these Content URIs?

解决方案

If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.

Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework

Edit

Here is how you can get an InputStream from your Uri

InputStream inputStream = getContentResolver().openInputStream(uri);

这篇关于从内容 URI 获取搜索图像的绝对文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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