By Priya Mall


2016-03-08 15:02:41 8 Comments

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?

2 comments

@CommonsWare 2016-03-08 15:08:23

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

@Priya Mall 2016-03-08 18:52:38

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?

@CommonsWare 2016-03-08 19:01:55

@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().

@Priya Mall 2016-03-10 05:37:24

Yes, you are right.Thank you ! I guess, can accept only one answer not both. So just commenting out..Accepted answer :)

@Priya Mall 2016-05-09 13:40:05

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?

@Scott Tomaszewski 2016-03-08 15:23:46

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);

@Priya Mall 2016-03-08 18:56:00

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?

@Scott Tomaszewski 2016-03-08 19:06:00

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 InputStream

@Scott Tomaszewski 2016-03-09 20:58:08

If this is correct answer, can you accept it? @Priya

@Priya Mall 2016-03-10 05:38:03

Done. Thank you!

@Priya Mall 2016-05-09 14:19:06

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?

@Scott Tomaszewski 2016-05-09 17:13:39

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!

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Get filename and path from URI from mediastore

11 Answered Questions

[SOLVED] How to get the Full file path from URI

  • 2012-11-03 13:10:33
  • tim687
  • 168163 View
  • 81 Score
  • 11 Answer
  • Tags:   android path get uri

21 Answered Questions

[SOLVED] Is there a way to get the source code from an APK file?

7 Answered Questions

[SOLVED] Convert content:// URI to actual path in Android 4.4

3 Answered Questions

[SOLVED] get path from Google Drive URI

11 Answered Questions

1 Answered Questions

How to extract the filetype of a content-URI

  • 2017-10-27 13:36:20
  • Tomhawk
  • 29 View
  • 0 Score
  • 1 Answer
  • Tags:   java android uri

3 Answered Questions

[SOLVED] Get a Content URI from a File URI?

  • 2014-04-21 23:15:02
  • selsine
  • 6639 View
  • 8 Score
  • 3 Answer
  • Tags:   android

Sponsored Content