Getting the Absolute File Path from Content URI for searched images
Asked Answered
C

2

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?

Clot answered 8/3, 2016 at 15:2 Comment(0)
O
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);
Octant answered 8/3, 2016 at 15:23 Comment(5)
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?Clot
As @CommmonsWare mentioned, you can upload an InputStream (github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-s3/…). I have updated my answer to include how to get the InputStreamOctant
If this is correct answer, can you accept it? @PriyaOctant
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 SO question. Can you please help?Clot
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!Octant
B
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
Bergamo answered 8/3, 2016 at 15:8 Comment(4)
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?Clot
@Priya: It sure looks like the Amazon S3 SDK supports InputStream for setting the contents of an S3Object. If so, you do not need a file. You just need the InputStream from openInputStream().Bergamo
Yes, you are right.Thank you ! I guess, can accept only one answer not both. So just commenting out..Accepted answer :)Clot
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 SO question. Can you please help?Clot

© 2022 - 2024 — McMap. All rights reserved.