简体   繁体   English

从内容 URI 获取搜索图像的绝对文件路径

[英]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可能指向:

  • A local file on external storage外部存储上的本地文件
  • A local file on internal storage for the other app其他应用程序的内部存储上的本地文件
  • A local file on removable storage可移动存储上的本地文件
  • A local file that is encrypted and needs to be decrypted on the fly已加密且需要即时解密的本地文件
  • A stream of bytes held in a BLOB column in a database数据库中BLOB列中保存的字节流
  • A piece of content that needs to be downloaded by the other app first需要其他应用先下载的一段内容
  • ...and so on ...等等

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM