6

I am trying to get image from third party app (e.g WhatsApp) to my app (being tested on Marshmallow). When I do "share image" from WhatsApp and share it with my app, I get URI something like this:

content://com.whatsapp.provider.media/item/61025

But in my app when I call getContentResolver().openInputStream(uri) or getContentResolver().openFileDescriptor(uri, "r") with above URI, it crashes with exception:

java.lang.SecurityException: Permission Denial: opening provider com.whatsapp.MediaProvider from ProcessRecord{a4b804a 30321:com.myapp/u0a145} (pid=30321, uid=10145) that is not exported from uid 10083

What I tried so far

I looked for this exception on SO and found similar question posed but to import images from Google Photos and got that need to add permission like:

<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>

But not convienced with this because there could be many apps and need to add permission for each of them or what.

In some other answers it has been suggested to read/copy the data from content provider immediately. But not sure how. Because I get exception in openInputStream itself.

I also must mention that WhatsApp image can be successfully shared with other apps (e.g. Google Drive) so there must be some way to do this.

Please someone can give share hints or working solution on this?

Atul
  • 3,778
  • 5
  • 47
  • 87
  • Do you have the permission to read from external storage and checked it during runtime? – Nabin Bhandari Oct 05 '17 at 06:19
  • that provider is not exported, only some limited subset of packages can access it (like google drive) but your app is not allowed to do that – pskink Oct 05 '17 at 08:11
  • @Nabin yes I have the permission – Atul Oct 05 '17 at 09:07
  • @pskink If that is the case then what would be the criteria. Because I tried with some other non Google apps (e.g. an image editor app InShot, File transfer Xender) and it works with them. Anyways is that documented anywhere please share link. Thanks. – Atul Oct 05 '17 at 09:13
  • Post the code you tried including manifest. Is this problem occurring while sharing from WhatsApp only or from other apps too? What about from gallery? – Nabin Bhandari Oct 05 '17 at 12:02
  • @Nabin What I observed is, this happens when my app gets shared image via content uri (`content://`) and not when it gets file uri (`file://`). Since gallary and other apps I tried shared their images using file uri so I didnt get exception. But WhatsApp gives me content uri of its images. – Atul Oct 05 '17 at 12:28
  • @Nabin: App code is too large to share. But there is nothing much as long as this crash is concerned. I simply pass input URI to `getContentResolver().openInputStream(uri)` and it crashes right there especially when uri is content uri from WhatsApp. – Atul Oct 05 '17 at 12:49

1 Answers1

4

Ok. I found where was the problem. In my app's initial activity I was storing content URI (Which I got from third party app like WhatsApp or Chrome etc. for e.g. content://com.whatsapp.provider.media/item/61025) in an array and was accessing it from other activity.

That was not correct. I got hint from this answer and I called getContentResolver().openInputStream(uri) right from the app's launcher activity immediately after I get shared contents. And it worked, didn't throw any exception.

Atul
  • 3,778
  • 5
  • 47
  • 87
  • can you provide sample code to get path fromcontent://com.whatsapp.provider.media/item/45856 – Rafiq Ahmad Oct 31 '17 at 07:09
  • do not try to get path from URI Its not correct way to deal with URIs. Instead use `getContentResolver().openInputStream(uri)` – Atul Oct 31 '17 at 09:55
  • getContentResolver().openInputStream(uri) what it returns ?? I am fully confused – Rafiq Ahmad Nov 03 '17 at 05:11
  • `getContentResolver().openInputStream` returns `InputStream` which you need to read for shared data. For example if it is image data you can call `BitmapFactory.decodeStream`. Please Google how to read data from `InputStream`. – Atul Nov 03 '17 at 09:13
  • 1
    Thanks for the explanation i solved it. It was returning bitmap from BitmapFactory.decodeStream and from there i got the path.Took 2 days whoa!! +1 for your help. – Rafiq Ahmad Nov 03 '17 at 10:15