Read and Write external storage permission isn't working

后端 未结 5 1628
佛祖请我去吃肉
佛祖请我去吃肉 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:28

    Recommended solution: User a permission library like Dexter or KotlinPermissions or RxPermissions. I have used all of them and they are quite reliable.

    Else:

    Write these lines in your activity, just bellow super.onCreate(...) and before setContentView(...):

    super.onCreate(savedInstanceState);
    
        // Here, thisActivity is the current activity
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        setContentView(R.layout.activity_main);
    

    Now it should work, it fixed the problem for me!

    0 讨论(0)
  • 2021-02-08 01:31

    From android 24 and above you need runtime permission to access some feature of device like Microphone and camera, storage etc. check you have storage permission access using below code

      private boolean hasStoragePermission(int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
    

    It show popup to allow for access, if its allow perform your action

     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (requestCode == FILE_ATTACHMENT)
                attachFile();
    
        }
    }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2021-02-08 01:42

    Clean the damn project, rebuild and run if it still does not work....

    Add this code in onCreate method in your activity. Delete the app, clean the project, build and run. Also do not forget to add the permission you want in your manifest. (For a lower API level the code is unnecessary but from 23 and up you also need this code)

    private fun requestPermission() {
    
        val permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        this.requestPermissions(permissions, 5)
    }
    
    0 讨论(0)
  • 2021-02-08 01:50

    in android API >= 23 you need to request permission at runtime. Take a look here

    Something like this

            // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
    
            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
    
                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            // Permission has already been granted
        }
    

    however, Ted permission library is a gread lib to avoid such boilerplate code.

    //call back after permission granted
        PermissionListener permissionlistener = new PermissionListener() {
            @Override
            public void onPermissionGranted() {
                Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onPermissionDenied(List<String> deniedPermissions) {
                Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
            }
    
    
        };
    
        //check all needed permissions together
        TedPermission.with(this)
                .setPermissionListener(permissionlistener)
                .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
                .setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .check();
    
    0 讨论(0)
提交回复
热议问题