mirror of
https://github.com/apache/cordova-plugin-camera.git
synced 2025-01-31 12:42:50 +08:00
CB-10120 android: Fix missing CAMERA permission for Android M
According to the PR conversation, when android.permission.CAMERA is not set in the package, there is no need to ask for the camera permission. Also, checking now camera and storage permissions separately, so if only one of them is missing, the other one will be requested and not both. Rebased by MatthewBooth and riknoll This closes #142, closes #174
This commit is contained in:
parent
826aca3524
commit
c12206ebc8
@ -58,6 +58,9 @@ import android.provider.MediaStore;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.PermissionInfo;
|
||||
|
||||
/**
|
||||
* This class launches the camera view, allows the user to take a picture, closes the camera view,
|
||||
* and returns the captured image. When the camera view is closed, the screen displayed before
|
||||
@ -105,7 +108,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
||||
private boolean orientationCorrected; // Has the picture's orientation been corrected
|
||||
private boolean allowEdit; // Should we allow the user to crop the image.
|
||||
|
||||
protected final static String[] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE };
|
||||
protected final static String[] permissions = { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE };
|
||||
|
||||
public CallbackContext callbackContext;
|
||||
private int numPics;
|
||||
@ -114,6 +117,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
||||
private Uri scanMe; // Uri of image to be added to content store
|
||||
private Uri croppedUri;
|
||||
|
||||
protected void getReadPermission(int requestCode)
|
||||
{
|
||||
cordova.requestPermission(this, requestCode, permissions[requestCode]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the request and returns PluginResult.
|
||||
*
|
||||
@ -229,14 +237,41 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
||||
* @param returnType Set the type of image to return.
|
||||
*/
|
||||
public void callTakePicture(int returnType, int encodingType) {
|
||||
if (PermissionHelper.hasPermission(this, permissions[0])) {
|
||||
boolean takePicturePermission = cordova.hasPermission(permissions[TAKE_PIC_SEC]);
|
||||
|
||||
if (!takePicturePermission) {
|
||||
takePicturePermission = true; // This permission is not required, unless we find android.permission.CAMERA in the package
|
||||
try {
|
||||
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
|
||||
String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
|
||||
if (permissionsInPackage != null) {
|
||||
for (String permission : permissionsInPackage) {
|
||||
if (permission.equals(Manifest.permission.CAMERA)) {
|
||||
takePicturePermission = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NameNotFoundException e) { }
|
||||
}
|
||||
|
||||
boolean saveAlbumPermission = cordova.hasPermission(permissions[SAVE_TO_ALBUM_SEC]);
|
||||
if (takePicturePermission && saveAlbumPermission) {
|
||||
takePicture(returnType, encodingType);
|
||||
} else {
|
||||
PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||
if (saveAlbumPermission && !takePicturePermission) {
|
||||
cordova.requestPermission(this, TAKE_PIC_SEC, permissions[TAKE_PIC_SEC]);
|
||||
}
|
||||
else if (!saveAlbumPermission && takePicturePermission) {
|
||||
cordova.requestPermission(this, TAKE_PIC_SEC, permissions[SAVE_TO_ALBUM_SEC]);
|
||||
}
|
||||
else
|
||||
{
|
||||
cordova.requestPermissions(this, TAKE_PIC_SEC, permissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void takePicture(int returnType, int encodingType)
|
||||
{
|
||||
// Save the number of images currently on disk for later
|
||||
|
Loading…
Reference in New Issue
Block a user