Skip to content

Commit 3509bd1

Browse files
committed
refactor(android): Rework permission management to make WRITE_EXTERNAL_STORAGE optional
1 parent c208754 commit 3509bd1

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ quality, even if a `quality` parameter is specified. To avoid common
167167
memory problems, set `Camera.destinationType` to `FILE_URI` rather
168168
than `DATA_URL`.
169169

170+
__NOTE__: To use `saveToPhotoAlbum` option on Android 9 (API 28) and lower, the `WRITE_EXTERNAL_STORAGE` permission must be declared.
171+
172+
To do this, add the following in your `config.xml`:
173+
174+
```xml
175+
<config-file target="AndroidManifest.xml" parent="/*" xmlns:android="http://schemas.android.com/apk/res/android">
176+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
177+
</config-file>
178+
```
179+
180+
Android 10 (API 29) and later devices does not require `WRITE_EXTERNAL_STORAGE` permission. If your application only supports Android 10 or later, then this step is not necessary.
181+
170182
__Supported Platforms__
171183

172184
- Android
@@ -263,7 +275,7 @@ Optional parameters to customize the camera settings.
263275
| targetHeight | <code>number</code> | | Height in pixels to scale image. Must be used with `targetWidth`. Aspect ratio remains constant. |
264276
| mediaType | <code>[MediaType](#module_Camera.MediaType)</code> | <code>PICTURE</code> | Set the type of media to select from. Only works when `PictureSourceType` is `PHOTOLIBRARY` or `SAVEDPHOTOALBUM`. |
265277
| correctOrientation | <code>Boolean</code> | | Rotate the image to correct for the orientation of the device during capture. |
266-
| saveToPhotoAlbum | <code>Boolean</code> | | Save the image to the photo album on the device after capture. |
278+
| saveToPhotoAlbum | <code>Boolean</code> | | Save the image to the photo album on the device after capture.<br />See [Android Quirks](#cameragetpicturesuccesscallback-errorcallback-options). |
267279
| popoverOptions | <code>[CameraPopoverOptions](#module_CameraPopoverOptions)</code> | | iOS-only options that specify popover location in iPad. |
268280
| cameraDirection | <code>[Direction](#module_Camera.Direction)</code> | <code>BACK</code> | Choose the camera to use (front- or back-facing). |
269281

src/android/CameraLauncher.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
193193
this.callTakePicture(destType, encodingType);
194194
}
195195
else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) {
196-
// FIXME: Stop always requesting the permission
197-
String[] permissions = getPermissions(true, mediaType);
198-
if(!hasPermissions(permissions)) {
199-
PermissionHelper.requestPermissions(this, SAVE_TO_ALBUM_SEC, permissions);
200-
} else {
201-
this.getImage(this.srcType, destType);
202-
}
196+
this.getImage(this.srcType, destType);
203197
}
204198
}
205199
catch (IllegalArgumentException e)
@@ -261,46 +255,57 @@ private String getTempDirectoryPath() {
261255
* @param encodingType Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
262256
*/
263257
public void callTakePicture(int returnType, int encodingType) {
264-
String[] storagePermissions = getPermissions(true, mediaType);
265-
boolean saveAlbumPermission;
266-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
267-
saveAlbumPermission = this.saveToPhotoAlbum ? hasPermissions(storagePermissions) : true;
268-
} else {
269-
saveAlbumPermission = hasPermissions(storagePermissions);
270-
}
271-
boolean takePicturePermission = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA);
272258

273259
// CB-10120: The CAMERA permission does not need to be requested unless it is declared
274260
// in AndroidManifest.xml. This plugin does not declare it, but others may and so we must
275261
// check the package info to determine if the permission is present.
262+
boolean manifestContainsCameraPermission = false;
276263

277-
if (!takePicturePermission) {
278-
takePicturePermission = true;
279-
try {
280-
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
281-
String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
282-
if (permissionsInPackage != null) {
283-
for (String permission : permissionsInPackage) {
284-
if (permission.equals(Manifest.permission.CAMERA)) {
285-
takePicturePermission = false;
286-
break;
287-
}
264+
// write permission is not necessary, unless if we are saving to photo album
265+
// On API 29+ devices, write permission is completely obsolete and not required.
266+
boolean manifestContainsWriteExternalPermission = false;
267+
268+
boolean cameraPermissionGranted = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA);
269+
boolean writeExternalPermissionGranted = false;
270+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
271+
writeExternalPermissionGranted = PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
272+
}
273+
else {
274+
writeExternalPermissionGranted = true;
275+
}
276+
277+
try {
278+
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
279+
String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
280+
if (permissionsInPackage != null) {
281+
for (String permission : permissionsInPackage) {
282+
if (permission.equals(Manifest.permission.CAMERA)) {
283+
manifestContainsCameraPermission = true;
284+
}
285+
else if (permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
286+
manifestContainsWriteExternalPermission = true;
288287
}
289288
}
290-
} catch (NameNotFoundException e) {
291-
// We are requesting the info for our package, so this should
292-
// never be caught
293289
}
290+
} catch (NameNotFoundException e) {
291+
// We are requesting the info for our package, so this should
292+
// never be caught
293+
}
294+
295+
ArrayList<String> requiredPermissions = new ArrayList<>();
296+
if (manifestContainsCameraPermission && !cameraPermissionGranted) {
297+
requiredPermissions.add(Manifest.permission.CAMERA);
294298
}
295299

296-
if (takePicturePermission && saveAlbumPermission) {
300+
if (saveToPhotoAlbum && !writeExternalPermissionGranted) {
301+
requiredPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
302+
}
303+
304+
if (!requiredPermissions.isEmpty()) {
305+
PermissionHelper.requestPermissions(this, TAKE_PIC_SEC, requiredPermissions.toArray(new String[0]));
306+
}
307+
else {
297308
takePicture(returnType, encodingType);
298-
} else if (saveAlbumPermission) {
299-
PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.CAMERA);
300-
} else if (takePicturePermission) {
301-
PermissionHelper.requestPermissions(this, TAKE_PIC_SEC, storagePermissions);
302-
} else {
303-
PermissionHelper.requestPermissions(this, TAKE_PIC_SEC, getPermissions(false, mediaType));
304309
}
305310
}
306311

0 commit comments

Comments
 (0)