0

When file is shared by other apps via FileProvider then the Uri received in my app is not readable. If i try to get file path or create file from it then it says file doesn't exist. The sample Uri is from whatsapp is content://com.whatsapp.provider.media/item/91836 . It gives Security exception when i try to read bitmap from the Uri.

Code lines is onCreate method:

Uri mediaUri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mediaUri);

Manifest:

<activity
        android:name=".ui.main.MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustNothing">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
            <data android:mimeType="video/mp4" />
            <!--<data android:mimeType="video/*" />-->
        </intent-filter>
    </activity>

Exception:

Caused by: java.lang.SecurityException: Permission Denial: opening provider com.whatsapp.MediaProvider from ProcessRecord{fbda37f 23891:com.fayvo/u0a273} (pid=23891, uid=10273) that is not exported from UID 10133
        at android.os.Parcel.createException(Parcel.java:1953)
        at android.os.Parcel.readException(Parcel.java:1921)
        at android.os.Parcel.readException(Parcel.java:1871)
        at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4159)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:6759)
        at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2912)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1879)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1460)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1313)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1033)
        at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:919)
Usman Rana
  • 2,067
  • 1
  • 21
  • 32
  • Please provide a [mcve]. This would include how you are getting the `Uri` and where you are trying to use the `Uri`. Bear in mind that [by default only the activity that receives the `Uri` can use it](https://commonsware.com/blog/2016/08/10/uri-access-lifetime-shorter-than-you-might-think.html). – CommonsWare Jul 10 '19 at 13:33
  • updated the code line, simply I share the picture from whatsapp chat and it doesn't load in my app – Usman Rana Jul 10 '19 at 13:41
  • If those two lines are sequential (e.g., in `onCreate()` of your `ACTION_VIEW` activity), and you are getting the `Uri` as shown from `EXTRA_STREAM`, then this would appear to be a bug in WhatsApp. – CommonsWare Jul 10 '19 at 13:42
  • But how other apps are working fine with it. My same code is working fine when i share the image from external storage of whatsapp. The issue seems with the uri that seems private in this case. – Usman Rana Jul 10 '19 at 13:56
  • "But how other apps are working fine with it" -- I cannot answer that. There may be bugs in your implementation, but we only have the two lines of code to look at, so it will be difficult for anyone to give you more specific advice. – CommonsWare Jul 10 '19 at 14:16
  • added further details, didn't post complete file as it will be messy and the main part of code that is handling the action is easy to read for viewers. – Usman Rana Jul 10 '19 at 14:21
  • Did you resolve this? I have the same issue – Marco Duindam Jul 03 '20 at 14:09
  • @MarcoDuindam I've added the method in the answer to this question, this worked for me in that case. – Usman Rana Jul 03 '20 at 14:19

1 Answers1

0

Using this method worked for me in those cases:

private File readSharedContentUri(Uri contentUri) {
    // Get content resolver.
    ContentResolver contentResolver = getContentResolver();
    File targetFile = null;
    try {
        InputStream inputStream = contentResolver.openInputStream(contentUri);
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        targetFile = PathUtil.getFilePath(context, true, "received_media_" + System.currentTimeMillis(), CommonUtils.isMimeTypeOfImage(contentResolver.getType(contentUri)) ? ".jpg" : ".mp4");
        inputStream.close();
        OutputStream outStream = new FileOutputStream(targetFile);
        outStream.write(buffer);
        outStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return targetFile;
}
Usman Rana
  • 2,067
  • 1
  • 21
  • 32