Christopher
Christopher

Reputation: 10269

Android 13 - READ_EXTERNAL_STORAGE_PERMISSION still usable

The behaviour changes for Android 13 mention this:

If your app targets Android 13, you must request one or more new permissions instead of the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions.

(https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions)

The new permissions are:

But how do I handle this, if I e.g. need to read PDF files from an arbitrary folder? There's no permission like READ_MEDIA_DOCUMENT or something like that. What about other file types, which are not images, videos or audio? Can I still use READ_EXTERNAL_STORAGE permission for them?

I didn't find any information about this in the official documentation, but to be honest the documentation focuses on media files only without any word about other file types (https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions).

I am also unsure about WRITE_EXTERNAL_STORAGE permission for other document types besides videos, images and audio.

Upvotes: 92

Views: 136743

Answers (8)

Li Zheng
Li Zheng

Reputation: 731

In my case, react-native-external-storage-permission solved this issue.

And as Use of All files access (MANAGE_EXTERNAL_STORAGE) permission - Play Console Help said, "you will be required to declare this and any other high risk permissions using the Declaration Form in Play Console"

Upvotes: 0

DevMukh
DevMukh

Reputation: 29

  /// Requests necessary permissions.
  Future<void> _requestPermissions() async {
    if (_permissionsGranted) return;

    if (Platform.isAndroid) {
      if (await _getAndroidVersion() >= 33) {
        var status = await Permission.manageExternalStorage.status;
        if (!status.isGranted) {
          status = await Permission.manageExternalStorage.request();
          if (!status.isGranted) {
            throw Exception("Manage External Storage permission not granted");
          }
        }
      } else {
        var status = await Permission.storage.status;
        if (!status.isGranted) {
          status = await Permission.storage.request();
          if (!status.isGranted) {
            throw Exception("Storage permission not granted");
          }
        }
      }
    }

    _permissionsGranted = true; // Update the permissions state
  }
these are the android menifest.yml file permissions
   <!-- Permissions for Android 13 and higher -->
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" android:required="false"/>
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" android:required="false"/>
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" android:required="false"/>

=> this solution works for me and hopefully will helpful for all of you. after using this must .flutter clean .flutter pub get .flutter run

Upvotes: 0

user21214172
user21214172

Reputation: 1

i faced an issue gallery select option to select pic from storage not accepting permission in some phones when i used "READ_EXTERNAL_STORAGE" and when i used "READ_MEDIA_IMAGES" so i asked **two permissions in manifest ** and i checked if user had any one permission it will give access of media based on phones android version

    val permissionStatus1 = ContextCompat.checkSelfPermission(this@EditCameraActivity, Manifest.permission.READ_MEDIA_IMAGES)
    val permissionStatus2 = ContextCompat.checkSelfPermission(this@EditCameraActivity, Manifest.permission.READ_EXTERNAL_STORAGE)

    if (permissionStatus1 != PackageManager.PERMISSION_GRANTED && permissionStatus2 != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this@EditCameraActivity, Manifest.permission.READ_MEDIA_IMAGES) ||
            ActivityCompat.shouldShowRequestPermissionRationale(this@EditCameraActivity, Manifest.permission.READ_EXTERNAL_STORAGE)) {

            ActivityCompat.requestPermissions(this@EditCameraActivity, arrayOf(
                Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_EXTERNAL_STORAGE
            ), REQUEST_PICK_IMAGE)
        } else {
            ActivityCompat.requestPermissions(this@EditCameraActivity, arrayOf(
                Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_EXTERNAL_STORAGE
            ), REQUEST_PICK_IMAGE)
        }
    } else {
        openGallery()
    }

Upvotes: 0

Ammar
Ammar

Reputation: 336

I you want permissions to access shared storage do the following

in AndroidManifest.xml declare these permissions

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> <!-- for android 11+ -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <!-- for android 10 and less -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- for android 10 and less -->

and in the application tag add the following attribute for android 10

<application
    android:requestLegacyExternalStorage="true"
    ...
    />

finally request runtime permission like this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // if android 11+ request MANAGER_EXTERNAL_STORAGE
    if (!Environment.isExternalStorageManager()) { // check if we already have permission
        Uri uri = Uri.parse(String.format(Locale.ENGLISH, "package:%s", getApplicationContext().getPackageName()));
        startActivity(
                new Intent(
                    Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
                    uri
                )
        );
    }
} else {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) { // check if we already have permission
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
        }, /* you request code here */);
    }
}

Upvotes: 1

naveed ahmed
naveed ahmed

Reputation: 470

On Android 13 the READ_EXTERNAL_STORAGE has been replaced by READ_MEDIA_IMAGES and READ_MEDIA_VIDEO

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 16

Akhtar Digital
Akhtar Digital

Reputation: 7

Try to reduce targetSdk from build.gradle

android {
namespace = "com.example.pdffile"
compileSdk = 33
defaultConfig {
    applicationId = "com.example.pdffile"
    minSdk = 25
    **targetSdk = 30**
    versionCode = 1
    versionName = "1.0"
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

Upvotes: -2

Tariq Mahmood
Tariq Mahmood

Reputation: 530

On Android 13 the READ_EXTERNAL_STORAGE has been replace by READ_MEDIA_IMAGES and READ_MEDIA_VIDEO.

In AndroidManifest.xml

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

And in your java file:

String[] permissions = new String[]{
    Manifest.permission.READ_MEDIA_VIDEO,
    Manifest.permission.READ_MEDIA_IMAGES,
    Manifest.permission.CAMERA
 };

Upvotes: 5

Alexander
Alexander

Reputation: 782

According to documentation (https://developer.android.com/training/data-storage/shared/media#storage-permission):

No permissions needed if you only access your own media files On devices that run Android 10 or higher, you don't need any storage-related permissions to access and modify media files that your app owns, including files in the Media Store. Downloads collection. If you're developing a camera app, for example, you don't need to request storage-related permissions because your app owns the images that you're writing to the media store.

From android 10 you can not to ask permissions to get files from storage. Works perfectly with all kinds of files (pdf, excel...) on my app on android 13 without any permissions. So you just need to ask READ_EXTERNAL_STORAGE permission for SDK, less than android 10.

But if you need special files (for example which was created by Instagram app but not stored in usual storage) you should ask for permission from your list.

Also look for types of Media storage: https://developer.android.com/reference/android/provider/MediaStore

About WRITE_EXTERNAL_STORAGE - you don`t need it since sdkVersion=29

EDIT: Was rewriting my app and want to add something:

It depends on what your app needs to do, but I have removed all kinds of storage permissions (only WRITE_EXTERNAL_STORAGE left for SDK less than 29) from my app, and just use ACTION_OPEN_DOCUMENT, ACTION_OPEN_DOCUMENT_TREE to have access for all kind of files (but not for system folders, ACTION_OPEN_DOCUMENT_TREE also have no access for download folder).

Upvotes: 63

Related Questions