Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 until I restart application (of course I allowed it in the dialog). After that it's working fine.

It's not a new app, I already declared WRITE_EXTERNAL_STORAGE in the manifest, and on earlier systems it's working fine.

I'm using checkSelfPermission and requestPermissions methods and when I make a read or write request to the sd card I've got an exception that I don't have permission.

Anyone has any idea what did I miss? Or did I run into a bug?

SDK version is the latest (updated a few hours ago), and I'm testing it on the emulator.

like image 941
Szörényi Ádám Avatar asked Aug 18 '15 21:08

Szörényi Ádám


People also ask

How do I get permission to write to external storage on Android?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

How do I give permission to internal storage on Android?

Storage permissions Android defines two permissions related to storage: READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE . As you can see, permissions are only defined for accessing external storage. That means that every app, by default, has permissions to access its internal storage.


1 Answers

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.

like image 140
Rajesh Avatar answered Oct 14 '22 14:10

Rajesh