Read and Write external storage permission isn't working

后端 未结 5 1626
佛祖请我去吃肉
佛祖请我去吃肉 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: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();
    
        }
    }
    

提交回复
热议问题