0

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?

Admin
  • 173
  • 3
  • 16
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this It would help if, instead of simply showing a `Toast`, you logged the exception via `Log.e()` or something. – CommonsWare Jul 01 '17 at 16:58

2 Answers2

1

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.

Koorosh
  • 457
  • 5
  • 14
0

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...

Suresh
  • 701
  • 1
  • 6
  • 20