Andrei Herford
Andrei Herford

Reputation: 18795

Android API 33 - How to get permission to read/write in external storage?

I am working on updating an existing Android app. The app offers a feature to store custom files (e.g. PDF repots, no image, audio or video) to the Documents folder on external storage `Documents/MyApp/Reports'. Additionally the app can read any such file from this folder, no matter if it was created by the app or uploaded by the user.

While the existing code works just fine up to API 32, no file permission dialog is shown when running on API 33. The handler onRequestPermissionsResult is called without any user interaction with denial for all requested permissions.

As @blackapps pointed out, on API 33 it is no longer necessary to ask for permissions to write and read files. While this seems to be true for files created by the app itself, I cannot access any files the user uploaded to Documents/MyApp/Reports. These files are simply not included in dir.listFiles().

How can I fix this?


I found other questions about similar issues and some of them link to this API 33 changes regarding "Granular media permissions". However, replacing READ_EXTERNAL_STORAGE with READ_MEDIA_IMAGES, READ_MEDIA_VIDEO and READ_MEDIA_AUDIO is not a good solution in my case since Allow XY to access music... would be misleading when storing a custom, non music file.

Additionally adding these permission to the manifest and requesting them within the app, does not change anything. Custom files uploaded by the user are still not included in dir.listFiles().

The same is true when add/requesting MANAGE_EXTERNAL_STORAGE permission instead.

How to correctly request external storage usage for custom / non-media files?


My code:

// AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


// SomeActivity.java
public class SomeActivity extends NavigationActivity {
    ...

    public boolean askForFilePermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            boolean hasPermission = this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

            if (!hasPermission) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                return true;
            }
        }

        return false;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
            // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay!
                
                } else {
                    // permission denied, boo!
                }

                return;
            }
        }
    }

    ...
}

Upvotes: 9

Views: 16662

Answers (1)

blackapps
blackapps

Reputation: 9292

For an Android 13 device you do not need to request write permissions as you have it by default.

So your app can just create folders and files in Documents directory and all other public directories.

Upvotes: 10

Related Questions