Skip to content

Commit 28c935c

Browse files
MauriceFrankmaurice.frank
andauthored
feat(android): add READ_MEDIA_* permissions for Android 13+ (apache#566)
* Add READ_MEDIA - permissions for Android 13+ * Fix spelling in permssion string --------- Co-authored-by: maurice.frank <[email protected]>
1 parent 1aaca40 commit 28c935c

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/android/FileUtils.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,25 @@ public class FileUtils extends CordovaPlugin {
100100
private PendingRequests pendingRequests;
101101

102102
/*
103-
* We need both read and write when accessing the storage, I think.
103+
* We need both read and write when accessing the storage, I think. (SDK Version < 33)
104+
*
105+
* If your app targets Android 13 (SDK 33) or higher and needs to access media files that other apps have created,
106+
* you must request one or more of the following granular media permissions
107+
* instead of the READ_EXTERNAL_STORAGE permission:
108+
*
109+
* READ_MEDIA_IMAGES
110+
* READ_MEDIA_VIDEO
111+
* READ_MEDIA_AUDIO
112+
*
113+
* Refer to: https://developer.android.com/about/versions/13/behavior-changes-13
104114
*/
105115

106116
private String [] permissions = {
107117
Manifest.permission.READ_EXTERNAL_STORAGE,
108-
Manifest.permission.WRITE_EXTERNAL_STORAGE };
118+
Manifest.permission.WRITE_EXTERNAL_STORAGE,
119+
Manifest.permission.READ_MEDIA_IMAGES,
120+
Manifest.permission.READ_MEDIA_VIDEO,
121+
Manifest.permission.READ_MEDIA_AUDIO};
109122

110123
// This field exists only to support getEntry, below, which has been deprecated
111124
private static FileUtils filePlugin;
@@ -577,7 +590,12 @@ public void run(JSONArray args) throws FileNotFoundException, JSONException, Mal
577590

578591
private void getReadPermission(String rawArgs, int action, CallbackContext callbackContext) {
579592
int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext);
580-
PermissionHelper.requestPermission(this, requestCode, Manifest.permission.READ_EXTERNAL_STORAGE);
593+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
594+
PermissionHelper.requestPermissions(this, requestCode,
595+
new String[]{Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO});
596+
} else {
597+
PermissionHelper.requestPermission(this, requestCode, Manifest.permission.READ_EXTERNAL_STORAGE);
598+
}
581599
}
582600

583601
private void getWritePermission(String rawArgs, int action, CallbackContext callbackContext) {
@@ -586,7 +604,13 @@ private void getWritePermission(String rawArgs, int action, CallbackContext call
586604
}
587605

588606
private boolean hasReadPermission() {
589-
return PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
607+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
608+
return PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_IMAGES)
609+
&& PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_VIDEO)
610+
&& PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_AUDIO);
611+
} else {
612+
return PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
613+
}
590614
}
591615

592616
private boolean hasWritePermission() {

0 commit comments

Comments
 (0)