Writing external storage doesn't work until I restart the application on Android M

前端 未结 1 1349
再見小時候
再見小時候 2020-12-11 00:33

I am having an issue with the new permission system. I modified my code to ask permission for WRITE_EXTERNAL_STORAGE. But my app cannot write to (or read from) the sd card u

相关标签:
1条回答
  • 2020-12-11 01:25

    You need Callback when User allow WRITE_EXTERNAL_STORAGE permission from RuntimePermission dialog.

    First you need to implement OnRequestPermissionsResultCallback Listener of ActivityCompat class in your Activity and override void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) Method to check whether User is allowing or denying the permission

    You can do like this:

    int REQUEST_STORAGE = 1;
    
    private void checkExternalStoragePermissions() {
        if (hasStoragePermissionGranted()) {
            //You can do what whatever you want to do as permission is granted
        } else {
            requestExternalStoragePermission();
        }
    }
    
    public boolean hasStoragePermissionGranted(){
            return  ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }
    
    public void requestExternalStoragePermission() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            ActivityCompat.requestPermissions(MainActivity.this, {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_STORAGE);
        }
    }
    

    Here is onRequestPermissionsResult() method will be called when user allow or deny Permission from Runtime permission dialog.

    You can also handle situation when User has checked never show Runtime Permission dialog.

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                               @NonNull int[] grantResults) {
    
            if (requestCode == REQUEST_STORAGE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   //User allow from permission dialog
                   //You can do what whatever you want to do as permission is granted
                } else if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivtity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                   //User has deny from permission dialog
                   Snackbar.make(mainLayout, getResources().getString("Please enable storage permission"),Snackbar.LENGTH_INDEFINITE)
                          .setAction("OK", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    ActivityCompat.requestPermissions(MainActivity.this, {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
                                }
                           })
                           .show();
                } else {
                    // User has deny permission and checked never show permission dialog so you can redirect to Application settings page
                    Snackbar.make(mainLayout, getResources().getString("Please enable permission from settings"),Snackbar.LENGTH_INDEFINITE)
                         .setAction("OK", new View.OnClickListener() {
                               @Override
                               public void onClick(View view) {
                                    Intent intent = new Intent();                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                    Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
                                    intent.setData(uri);
                                    startActivity(intent);
                                }
                           })
                           .show();
                }
            }
    }
    

    Here i have used Snackbar to show relevant message to user about Permission and mainLayout is id of Activity's Main Layout.

    Hope it helps you.

    0 讨论(0)
提交回复
热议问题