Android: getting a bitmap from a third-party application (e.g. WhatsApp) via content: // URI

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

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

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

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

What have i tried so far

I looked for this exception in SO and found a similar question , but I imported images from Google Photos and got the need to add permission, for example:

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

But I do not agree with this, because there can be many applications and you need to add permissions for each of them or what.

Some other answers suggested reading / copying data from the content provider immediately. But I don’t know how to do it. Because I get an exception in myself openInputStream.

I should also mention that the WhatsApp image can be successfully exchanged with other applications (e.g. Google Drive), so there must be some way to do this.

Please can anyone give a hint or working solution on this?

+4
2

Ok. , . URI ( , WhatsApp Chrome .., , ://com.whatsapp.provider.media/item/61025) .

. , getContentResolver().openInputStream(uri) . , .

+1

InputStream URI, WhatsApp . .

Intent intent = getIntent();
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
InputStream is = getContentResolver().openInputStream(imageUri);

: - , - .

0

Source: https://habr.com/ru/post/1686931/


All Articles