[英]Getting the Absolute File Path from Content URI for searched images
I am trying to share images from other applications to my application using implicit intent ACTION_SEND .我正在尝试使用隐式意图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
在从 chrome 浏览器共享搜索图像时,应用程序接收带有内容 URI 的意图,如下所示: content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI?如何从这种类型的内容 URI 中获取文件路径? All other apps like Facebook, Google+ are doing it. Facebook、Google+ 等所有其他应用程序都在这样做。 I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).我正在使用FileChooser来获取其他类型的内容 URI(例如来自 Gallery)的文件路径。
Tried looking everywhere, without much help.尝试到处寻找,没有太多帮助。 Can someone suggest how to work with these Content URIs?有人可以建议如何使用这些内容 URI 吗?
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.如果您绝对需要文件的本地副本,您将需要打开InputStream
将内容复制到您知道路径的本地文件,然后从那里开始。 Sidenote: Guava's ByteStreams#copy
is an easy way to accomplish this.旁注:Guava 的ByteStreams#copy
是实现此目的的简单方法。
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want.当然,这个文件不再受原始 Uri 源的支持,所以我认为这不是你想要的。 Instead, you should work with the Uri's intended API.相反,您应该使用 Uri 的预期 API。 Take a look at the Storage Access Framework查看存储访问框架
Edit编辑
Here is how you can get an InputStream
from your Uri
这是从Uri
获取InputStream
的方法
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?如何从这种类型的内容 URI 中获取文件路径?
You don't, as there does not have to be a file at all behind the Uri
, let alone one that you can access.您不需要,因为Uri
后面根本不需要文件,更不用说您可以访问的文件了。 That Uri
might point to:那个Uri
可能指向:
BLOB
column in a database数据库中BLOB
列中保存的字节流All other apps like Facebook, Google+ are doing it Facebook、Google+ 等所有其他应用程序都在这样做
No, they are not.不,他们不是。 They are using ContentResolver
and:他们正在使用ContentResolver
并且:
openInputStream()
to read in the bytes associated with the content openInputStream()
读取与内容关联的字节getType()
to get the MIME type associated with the content getType()
获取与内容关联的 MIME 类型query()
and the OpenableColumns
to get the size and display name associated with the content query()
和OpenableColumns
以获取与内容关联的大小和显示名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.