Search code examples
androidurifile-descriptor

changing activity(context) affect on access permision to received uri?


I'm trying to receive file from 'share menu' outside of my application and then process on it. I receive Uri and convert it to fileDescriptor, put it in static variable and then use that variable in another activity. The problem is at first activity, every thing works well (like getting thumbnail with MetaDataRetriever) but in the next activity and with same method it gives error java.lang.RuntimeException: setDataSource failed: status = 0x80000000. Same happens if I use URI directly. I know I can do all on same activity, but prefer to do it on separated activities.

public static Bitmap videoThumbMini(FileDescriptor descriptor) {
        Bitmap bitmap = null;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(descriptor);
            bitmap = retriever.getFrameAtTime(1000);
            if (bitmap == null)
                return null;
            float ratio = 1;
            if (bitmap.getHeight() > 300)
                ratio = bitmap.getHeight() / 300f;
            bitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() / ratio), 200, false);
        } catch (RuntimeException e) {
            Logger.log(e.getMessage() + " ERR3", false, e);
        } finally {
            try {
                retriever.release();
            } catch (Exception e) {
                Logger.log(e.getMessage() + " ERR4", false, e);
            }
        }
        return bitmap;
    }

Solution

  • for who come later, this makes it work properly: https://developer.android.com/guide/topics/manifest/grant-uri-permission-element

    this method should run in your first activity and allow whole your app ti use that uri.

    grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

    set packagename as same as your app.