8

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?

Priya Mall
  • 581
  • 1
  • 10
  • 22

2 Answers2

6

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);
Scott Tomaszewski
  • 1,009
  • 8
  • 27
  • No, You are right.Maybe, this is what I want! I have to attach image to a post and upload to s3 bucket in my application.And for doing this I will be needing filepath. Thanks for the suggestion, will try it out. Will there be some performance issue if there are multiple images to be attached using this approach? – Priya Mall Mar 08 '16 at 18:56
  • As @CommmonsWare mentioned, you can upload an `InputStream` (https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-s3/src/main/java/com/amazonaws/services/s3/model/S3Object.java#L107-L117). I have updated my answer to include how to get the `InputStream` – Scott Tomaszewski Mar 08 '16 at 19:06
  • If this is correct answer, can you accept it? @Priya – Scott Tomaszewski Mar 09 '16 at 20:58
  • I was using openInputStream and it worked fine.But I tested it again and now its throwing Security Exception.Facing exact same issue like [this](http://stackoverflow.com/questions/36544838/im-received-the-output-path-that-i-cant-access-because-its-custom-content-pro) SO question. Can you please help? – Priya Mall May 09 '16 at 14:19
  • You should open a second question and post the code you have, the stack trace you get, and a description of the question. This will be easier for people to answer and also help future developers with similar questions. Thanks! – Scott Tomaszewski May 09 '16 at 17:13
4

How can I fetch file path from this type of Content 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. That Uri might point to:

  • 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
  • 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

No, they are not. They are using ContentResolver and:

  • openInputStream() to read in the bytes associated with the content
  • getType() to get the MIME type associated with the content
  • query() and the OpenableColumns to get the size and display name associated with the content
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for explaining the concept, now I understand it clearly. Actually, I have to attach image to a post and upload to s3 bucket in my application.And for doing this I will be needing filepath. I think, I should try @Scotty's suggestion, what do you suggest? – Priya Mall Mar 08 '16 at 18:52
  • 1
    @Priya: It sure looks like the [Amazon S3 SDK supports `InputStream` for setting the contents of an `S3Object`](https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-s3/src/main/java/com/amazonaws/services/s3/model/S3Object.java#L107-L117). If so, you do not need a file. You just need the `InputStream` from `openInputStream()`. – CommonsWare Mar 08 '16 at 19:01
  • Yes, you are right.Thank you ! I guess, can accept only one answer not both. So just commenting out..Accepted answer :) – Priya Mall Mar 10 '16 at 05:37
  • I was using openInputStream and it worked fine.But I tested it again and now its throwing Security Exception.Facing exact same issue like [this](http://stackoverflow.com/questions/36544838/im-received-the-output-path-that-i-cant-access-because-its-custom-content-pro) SO question. Can you please help? – Priya Mall May 09 '16 at 13:40