Skip to content Skip to sidebar Skip to footer

Application Not Able To Access Sd Card When Write_external_storage Permission Is Granted At Run Time

In android M to access sdcard has to force stop and start app manually when permission is granted at runtime, how to achieve it programmatically? However sdcard can be accessed if

Solution 1:

You will need to restart the application to obtain the WRITE_EXTERNAL_STORAGE permission (an some other permissions). This is because this permission is actually a Linux permission. The latest preview version of Android does not restart the application in this case, but maybe Google will add this later.

You could use this code to do the restart:

@OverridepublicvoidonRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    // Write external store permission requires a restartfor (inti=0; i < permissions.length; i++)
        if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[i]) &&
                grantResults[i] == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "Restarting application");

            // Schedule start after 1 secondPendingIntentpi= PendingIntent.getActivity(
                    this,
                    0,
                    getIntent(),
                    PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManageram= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pi);

            // Stop now
            System.exit(0);
        }
}

Edit: you can find a list of Android permissions which are associated with a Linux permission here: https://android.googlesource.com/platform/frameworks/base/+/master/data/etc/platform.xml

Solution 2:

You can below condition to check whether SDCard is available or not

if (android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED)) {

 //Check for the file
 File appFolder = new File(Environment.getExternalStorageDirectory() + File.separator
            + context.getString(R.string.app_name));

 boolean exist = appFolder.exists();
}

Post a Comment for "Application Not Able To Access Sd Card When Write_external_storage Permission Is Granted At Run Time"