Getting the Absolute File Path from Content URI for searched images(从内容 URI 中获取搜索图像的绝对文件路径)
问题描述
我正在尝试使用隐式意图将其他应用程序中的图像共享到我的应用程序 ACTION_SEND.
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
在从 chrome 浏览器共享搜索图像时,应用会收到带有如下内容 URI 的意图:content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
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
如何从这种类型的 Content URI 中获取文件路径? Facebook、Google+ 等所有其他应用程序都在这样做.我正在使用 FileChooser 获取其他类型内容 URI 的文件路径(例如,来自图库).
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).
尝试到处寻找,但没有太多帮助.有人可以建议如何使用这些内容 URI 吗?
Tried looking everywhere, without much help. Can someone suggest how to work with these Content URIs?
推荐答案
如果你绝对需要文件的本地副本,你将需要打开 InputStream
将内容复制到本地您知道路径的文件,然后从那里开始.旁注:Guava 的 ByteStreams#copy
是实现此目的的简单方法.
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.
当然,这个文件不再由原始 Uri 源支持,所以我认为这不是你想要的.相反,您应该使用 Uri 的预期 API.看看 存储访问框架
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
编辑
以下是如何从 Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
这篇关于从内容 URI 中获取搜索图像的绝对文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!