I have updated my app using guidance elsewhere on this forum to use the Android 6.0 permissions model for accessing external SD cards. While the app/permissions work fine when installing the app from new, if I upgrade the app from the previous version (which relied on permissions set in AndroidManifest.xml) the app gets refused access to the SD card - I need to uninstall the existing version and re-install it to regain access.
Can anyone explain why this app update "breaks" external SD card permissions when I replace an already-installed version of the app?
The code I have used (in the onCreate method of my MainActivity) is:
if (Build.VERSION.SDK_INT >= 23) {
// For Marshmallow devices check permissions for accessing External SD card
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
logger.info("Permission is granted");
return;
} else {
logger.info("Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return;
}
}
else { //permission is automatically granted on sdk<23 upon installation
logger.info("Permission is granted");
return;
}
}
Edit: This code returns "InvalidSourceFolder" when the app has been upgraded from the previous release on a Lollipop device:
File f = new File("/storage/external_SD/DCIM/Camera");
File file[] = f.listFiles();
//If file is null then the source folder isn't valid
if (file == null) {
return "InvalidSourceFolder";
}