Read and Write external storage permission isn't working

后端 未结 5 1627
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-08 00:58

In my app i want to get user permission to read and write data on external storage, i have put permission tag in my manifest as below. But when installing app or running and whe

5条回答
  •  后悔当初
    2021-02-08 01:41

    Read and Write permission for storage and gallery usage for marshmallow or above:

      private void requestPermission() {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              requestPermissions(new String[] {
                  android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
           } else {
               openFilePicker();
           }
      }
    

    To check permission result use below method:

     @Override
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
                                            permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
        switch (requestCode) {
            case STORAGE_PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // check whether storage permission granted or not.
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        // do what you want;
                    }
                }
                break;
            default:
                break;
        }
    }
    

提交回复
热议问题