question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't handle MediaStore URI if no Storage Permission.

See original GitHub issue

If Storage Permission is not granted, fetchDecodedImage in ImagePipeline fails when passed an ImageRequest with a MediaStore URI (e.g. content://media/external/images/media/917).

Error is /storage/emulated/0/DCIM/Camera/20161128_142252.jpg: open failed: EACCES (Permission denied)

My workaround was to convert the MediaStore Uri into a File Uri that is saved in the cache directory. The File Uri is then passed to Fresco.

if (UriUtil.isLocalCameraUri(uri)) {
   uri = copyMediaStoreUriToCacheDir(uri, filename);
}

    Uri copyMediaStoreUriToCacheDir(Uri uri, String filename) {

        String destinationFilename = App.getAppContext().getCacheDir().getAbsolutePath() + "/" + filename;

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(App.getAppContext().getContentResolver().openInputStream(uri));
            bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
            byte[] buf = new byte[1024];
            bis.read(buf);
            do {
                bos.write(buf);
            } while (bis.read(buf) != -1);

            return Uri.fromFile(new File(destinationFilename));
        } catch (IOException e) {
            //
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                //
            }
        }

        return null;
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dannyroacommented, Feb 10, 2017

@oprisnik: Sorry. Another thing I left out is it only happens in certain devices like LG G4 and Samsung Galaxy S6 - 6.0.1.

screen shot 2017-02-10 at 7 54 04 am

Same code in my Google Pixel works fine.

1reaction
oprisnikcommented, Feb 9, 2017

Thanks, I only tested ACTION_OPEN_DOCUMENT and ACTION_PICK (sample will be available soon). I’ll test with ACTION_GET_CONTENT.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access media files from shared storage | Android Developers
If scoped storage is enabled, the collection shows only the photos, videos, and audio files that your app has created. Most developers won't...
Read more >
How to get permanent access to file URI [duplicate]
the error I get: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.
Read more >
Scoped Storage Stories: More on ... - CommonsWare
The biggest problem is that RecoverableSecurityException is on a per- Uri basis. The exception contains an IntentSender that can be used to get ......
Read more >
Working with Scoped Storage - ProAndroidDev
In a nutshell, Scoped Storage prevents apps from having unrestricted access to the filesystem on the device . Previously, an app could access...
Read more >
storage permission in android 11 | read & write both - YouTube
Hello everyoneIn this video i will teach you how to take storage persmission in android 11 for both read & write operation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found