Android: Getting image bitmap from third party app (e.g. WhatsApp) via content:// URI

社会主义新天地 提交于 2019-11-28 01:14:57

问题


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.



来源:https://stackoverflow.com/questions/46578918/android-getting-image-bitmap-from-third-party-app-e-g-whatsapp-via-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!