diff --git a/src/android/Capture.java b/src/android/Capture.java index 15f39481..7a07aedf 100644 --- a/src/android/Capture.java +++ b/src/android/Capture.java @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one import android.Manifest; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; @@ -74,10 +75,11 @@ public class Capture extends CordovaPlugin { private static final String LOG_TAG = "Capture"; private static final int CAPTURE_INTERNAL_ERR = 0; -// private static final int CAPTURE_APPLICATION_BUSY = 1; + // private static final int CAPTURE_APPLICATION_BUSY = 1; // private static final int CAPTURE_INVALID_ARGUMENT = 2; private static final int CAPTURE_NO_MEDIA_FILES = 3; private static final int CAPTURE_PERMISSION_DENIED = 4; + private static final int CAPTURE_NOT_SUPPORTED = 20; private boolean cameraPermissionInManifest; // Whether or not the CAMERA permission is declared in AndroidManifest.xml @@ -229,13 +231,17 @@ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean vi * Sets up an intent to capture audio. Result handled by onActivityResult() */ private void captureAudio(Request req) { - if (!PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { - PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.READ_EXTERNAL_STORAGE); - } else { - Intent intent = new Intent(android.provider.MediaStore.Audio.Media.RECORD_SOUND_ACTION); + if (!PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { + PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.READ_EXTERNAL_STORAGE); + } else { + try { + Intent intent = new Intent(android.provider.MediaStore.Audio.Media.RECORD_SOUND_ACTION); - this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode); - } + this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode); + } catch (ActivityNotFoundException ex) { + pendingRequests.resolveWithFailure(req, createErrorObject(CAPTURE_NOT_SUPPORTED, "No Activity found to handle Audio Capture.")); + } + } } private String getTempDirectoryPath() { @@ -254,16 +260,16 @@ private String getTempDirectoryPath() { */ private void captureImage(Request req) { boolean needExternalStoragePermission = - !PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); + !PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); boolean needCameraPermission = cameraPermissionInManifest && - !PermissionHelper.hasPermission(this, Manifest.permission.CAMERA); + !PermissionHelper.hasPermission(this, Manifest.permission.CAMERA); if (needExternalStoragePermission || needCameraPermission) { if (needExternalStoragePermission && needCameraPermission) { - PermissionHelper.requestPermissions(this, req.requestCode, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}); + PermissionHelper.requestPermissions(this, req.requestCode, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}); } else if (needExternalStoragePermission) { - PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.READ_EXTERNAL_STORAGE); + PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); } else { PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.CAMERA); } @@ -517,11 +523,11 @@ private JSONObject createErrorObject(int code, String message) { */ private Cursor queryImgDB(Uri contentStore) { return this.cordova.getActivity().getContentResolver().query( - contentStore, - new String[] { MediaStore.Images.Media._ID }, - null, - null, - null); + contentStore, + new String[] { MediaStore.Images.Media._ID }, + null, + null, + null); } /** diff --git a/types/index.d.ts b/types/index.d.ts index 726c5dac..17f53858 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -123,6 +123,7 @@ declare var CaptureError: { CAPTURE_INVALID_ARGUMENT: number; CAPTURE_NO_MEDIA_FILES: number; CAPTURE_NOT_SUPPORTED: number; + CAPTURE_PERMISSION_DENIED: number; } /** Encapsulates audio capture configuration options. */