Android unable to ask storage permission for android 13 devices

197 Views Asked by At

In my Android project, I am asking the user to open the camera. Along with that, I am also asking to ask for storage permission. The permission is working for Android version 12 and less but for Android versions greater than 12, it's not working.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Fragment

siteSnapButton.setOnClickListener(v -> {
        if (siteImagesCount >= 10) {
            Common.showToast(getActivity(), "you have taken maximum site snaps", Toast.LENGTH_LONG);
            return;
        }
        currentCameraActionRequest = Constants.REQUEST_TAKE_PHOTO_SITE;
        if (checkCameraPermission()) {
            if (checkStoragePermission()) {
                openCamera();
            }
        }
    });

public boolean checkStoragePermission() {
    if (Build.VERSION.SDK_INT < 23) {
        return true;
    }
    int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        // We don't have storage permission; ask for it
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // User previously denied permission; explain why it is required
            showAlertDialogForPermissions("Storage Permission is required",
                    "You previously denied storage permission, but it is needed in order to save images and files. Go to permissions?");
        } else {
            // Request the permission
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    Constants.MY_PERMISSIONS_STORAGE_REQUEST_FOR_CAMERA_CODE);

        }
        return false;
    } else {
        return true;
    }
}

Constant Class

public static final int MY_PERMISSIONS_STORAGE_REQUEST_FOR_CAMERA_CODE = 777;

Any help would be highly appreciated.

1

There are 1 best solutions below

0
Andrew On

If you read the documentation for the permissions

WRITE_EXTERNAL_STORAGE

Note: If your app targets Build.VERSION_CODES.R or higher, this permission has no effect.

READ_EXTERNAL_STORAGE

Note: Starting in API level 33, this permission has no effect. If your app accesses other apps' media files, request one or more of these permissions instead: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO. Learn more about the storage permissions that are associated with media files.

You can see that Google have restricted the effectiveness of these permissions by giving them both no effect.

You are not restricted from writing your files to external storage.

So a possible solution is to change checkStoragePermission to

if (Build.VERSION.SDK_INT < 23 || Build.VERSION.SDK_INT > 32) {
        return true;
    }

As you have already set in your Manifest the maximum api level they will work at to android:maxSdkVersion="32"