2

I have an app that stores it's images on the external storage. This have been working great up until I tried the new M preview.

I use picasso (I even tried Ion) to load the images and I get the images with the "file:///mnt/sdcard/appname/image1.jpg" URI.

I don't get any errors at all, but I'm guessing that M has changed the permissions to read from external storage. I have tried googling but I come up empty.

Writing the images to the external storage works just as normal by the way.

Jonathan Andersson
  • 1,057
  • 1
  • 10
  • 19

2 Answers2

1

Although siris_cac answer was very well explained it wasn't what caused my problem. Turns out I can't use "file:///mnt/sdcard/appname/image1.jpg" to load the file from external storage in M.

I guess it might have something to to with http://www.androidpolice.com/2015/05/28/android-m-feature-spotlight-external-storage-can-be-adopted-as-true-internal-storage-or-accessed-normally-with-no-additional-apps/

I guess I was wrong from the begining not using the Enviroment.getExternalStorageDirectory() when loading images, only when saving them.

So now to load images I use:

"file:///" + Environment.getExternalStorageDirectory().getPath() + "/appname/image1.jpg";
Jonathan Andersson
  • 1,057
  • 1
  • 10
  • 19
0

You could try checking if the permission is granted by :

checkCallingOrSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);

And try requesting permissions on Runtime like this using requestPermissions().

int EXT_PERMISSION = 1;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);

And, Handle the Permission Granted result

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case EXT_PERMISSION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              //permission granted
            } else {
              //permission denied
            }
            break;
        }
    }
}
siriscac
  • 1,699
  • 12
  • 21
  • I haven't changed anything though. To my understanding the new permission is only if you build against M? – Jonathan Andersson Jun 01 '15 at 12:43
  • Yes. It's only if your'e targeting M. I suppose the same code which you've now, works on previous versions. – siriscac Jun 01 '15 at 13:04
  • Yes it works great on old versions of android. But do you mean that pretty much all apps will crash or get unexpected behavior on M unless they get an update to build with M SDK ? Because I can't even use the requestPermissions method with my old SDK? – Jonathan Andersson Jun 01 '15 at 14:14
  • I faced the similar problem on WhatsApp. The app would just open a Camera instance but not a ImagePicker or a Gallery instance. You can very well add a check condition if the Build version is greater than M before calling `requestPermissions()`. – siriscac Jun 01 '15 at 14:17