Android: Getting image bitmap from third party app

2019-02-15 04:53发布

问题:

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?

回答1:

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.