简体   繁体   中英

android.Manifest.permission.WRITE_EXTERNAL_STORAGE - application crashes

I am checking permission if my android application have permissions on WRITE_EXTERNAL_STORAGE. I have already added in application manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I am using Code :

 try{
           boolean hasPermission =this.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
           if ( hasPermission) {
               Log.v("AppPerm", "Permission is granted");


               Toast.makeText(getBaseContext(), "Permission is granted", Toast.LENGTH_LONG)
                       .show();
           }
           else {
               msgbox("Error:Storage Permission Not Granted");
               Toast.makeText(getBaseContext(), "Error:Storage Permission Not Granted", Toast.LENGTH_LONG)
                       .show();
           }
       }catch (Exception e){
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                       .show();
       }

When this code is executed (in android version 5.1 ) the Application Crashes with error :

unfortunately, the application has stopped.

I don't want the application to crash, It is not Catching the error?

Like Google documentation :

  // Assume thisActivity is the current activity

        int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED , and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED , and the app has to explicitly ask the user for permission.

Update: This was useful for me:

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission. WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("tag","Permission is granted");
            return true;
        } else {

            Log.v("tag","Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission. WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v("tag","Permission is granted");
        return true;
    }


}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v("tag","Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}

I hope it will be useful for you, too.

You have spilt the permission checking part using SDK version.

You don't need run time permission before lollipop. So get the SDK version and put it on if condition.

Hope it will help you...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM