Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - 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 the app is force stop and restarted.

AndroidManifest.xml :

uses-permission-sdk-m android:name="android.permission.WRITE_EXTERNAL_STORAGE


     /* **Checking permission **
        if doesn't have request
         else
         browse file directory       
    */

    String[] perm = { Manifest.permission.WRITE_EXTERNAL_STORAGE };
    if (checkSelfPermission(perm[0]) == PackageManager.PERMISSION_DENIED) {


                    requestPermissions(perm, REQ);

                } else {

                    Explore(); //method to browse file directory
                }

onRequestPermissionsResult

   @Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    // TODO Auto-generated method stub

    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  

   //Not able to browse SD card until restart application


       Explore(); //method to browse file directory


    } else {
        Log.i("onRequestPermissionsResult", " Request denied");
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    // Write external store permission requires a restart
    for (int i = 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 second
            PendingIntent pi = PendingIntent.getActivity(
                    this,
                    0,
                    getIntent(),
                    PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am = (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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...