0

I've been trying for several hours to open an image from Android's external storage with Intent.ACTION_VIEW. And as you might have figured out already, I'm still not able to do that.

I've searched all over Google and I've tried a lot of ways to make this work, however none of them did. All what I could find are questions that are up to 1 year old. I tried using FileProvider and here's what I ended up with:

File imagePath = new File(Environment.getExternalStorageDirectory(), "LyceeLamartine/cache");
File newFile = new File(imagePath, "calendrier.png");
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "ml.toufic.lyceelamartine.fileprovider", newFile);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "image/*");
startActivity(intent);

The code above opens Google Photos successfully but it shows a black screen and the path seems to be: content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png and this seems like an internal storage path.

The image I want to access is stored at: "/storage/emulated/0/LyceeLamartine/cache/calendrier.png" and I'm using a phone with Android O (8.0).

I'm still a beginner in Java and any help would be greatly appreciated!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Toufic Batache
  • 762
  • 10
  • 24
  • see hear if helpful for u :- https://stackoverflow.com/questions/15918335/read-an-image-file-from-external-storage-android – Ali Feb 10 '18 at 16:29
  • Thanks Mohammad for your fast reply but, unfortunately, I'm not trying to open an image inside an `imageView` when I really want to use `intent` to open it and it's from 2013. – Toufic Batache Feb 10 '18 at 16:33

1 Answers1

2

the path seems to be: content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png and this seems like an internal storage path.

That is not a path. That is a Uri. It has a scheme (content). It points to your FileProvider (ml.toufic.lyceelamartine.fileprovider). It seems fine — if there were a problem with the Uri, FileProvider would have thrown an exception when you tried to create it.

The code above opens Google Photos successfully but it shows a black screen

There are two problems with your Intent.

First, do not use a wildcard MIME type. It is your file. You know what the MIME type is. Use the actual MIME type (in this case, image/png).

Second, call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) on the Intent as part of setting it up, before calling startActivity(). Right now, no app has rights to use your content.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491