@@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2020
2121import android .Manifest ;
2222import android .app .Activity ;
23+ import android .content .ActivityNotFoundException ;
2324import android .content .ContentResolver ;
2425import android .content .ContentValues ;
2526import android .content .Context ;
@@ -74,10 +75,11 @@ public class Capture extends CordovaPlugin {
7475 private static final String LOG_TAG = "Capture" ;
7576
7677 private static final int CAPTURE_INTERNAL_ERR = 0 ;
77- // private static final int CAPTURE_APPLICATION_BUSY = 1;
78+ // private static final int CAPTURE_APPLICATION_BUSY = 1;
7879// private static final int CAPTURE_INVALID_ARGUMENT = 2;
7980 private static final int CAPTURE_NO_MEDIA_FILES = 3 ;
8081 private static final int CAPTURE_PERMISSION_DENIED = 4 ;
82+ private static final int CAPTURE_NOT_SUPPORTED = 20 ;
8183
8284 private boolean cameraPermissionInManifest ; // Whether or not the CAMERA permission is declared in AndroidManifest.xml
8385
@@ -229,13 +231,17 @@ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean vi
229231 * Sets up an intent to capture audio. Result handled by onActivityResult()
230232 */
231233 private void captureAudio (Request req ) {
232- if (!PermissionHelper .hasPermission (this , Manifest .permission .READ_EXTERNAL_STORAGE )) {
233- PermissionHelper .requestPermission (this , req .requestCode , Manifest .permission .READ_EXTERNAL_STORAGE );
234- } else {
235- Intent intent = new Intent (android .provider .MediaStore .Audio .Media .RECORD_SOUND_ACTION );
234+ if (!PermissionHelper .hasPermission (this , Manifest .permission .READ_EXTERNAL_STORAGE )) {
235+ PermissionHelper .requestPermission (this , req .requestCode , Manifest .permission .READ_EXTERNAL_STORAGE );
236+ } else {
237+ try {
238+ Intent intent = new Intent (android .provider .MediaStore .Audio .Media .RECORD_SOUND_ACTION );
236239
237- this .cordova .startActivityForResult ((CordovaPlugin ) this , intent , req .requestCode );
238- }
240+ this .cordova .startActivityForResult ((CordovaPlugin ) this , intent , req .requestCode );
241+ } catch (ActivityNotFoundException ex ) {
242+ pendingRequests .resolveWithFailure (req , createErrorObject (CAPTURE_NOT_SUPPORTED , "No Activity found to handle Audio Capture." ));
243+ }
244+ }
239245 }
240246
241247 private String getTempDirectoryPath () {
@@ -254,16 +260,16 @@ private String getTempDirectoryPath() {
254260 */
255261 private void captureImage (Request req ) {
256262 boolean needExternalStoragePermission =
257- !PermissionHelper .hasPermission (this , Manifest .permission .READ_EXTERNAL_STORAGE );
263+ !PermissionHelper .hasPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE );
258264
259265 boolean needCameraPermission = cameraPermissionInManifest &&
260- !PermissionHelper .hasPermission (this , Manifest .permission .CAMERA );
266+ !PermissionHelper .hasPermission (this , Manifest .permission .CAMERA );
261267
262268 if (needExternalStoragePermission || needCameraPermission ) {
263269 if (needExternalStoragePermission && needCameraPermission ) {
264- PermissionHelper .requestPermissions (this , req .requestCode , new String []{Manifest .permission .READ_EXTERNAL_STORAGE , Manifest .permission .CAMERA });
270+ PermissionHelper .requestPermissions (this , req .requestCode , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE , Manifest .permission .CAMERA });
265271 } else if (needExternalStoragePermission ) {
266- PermissionHelper .requestPermission (this , req .requestCode , Manifest .permission .READ_EXTERNAL_STORAGE );
272+ PermissionHelper .requestPermission (this , req .requestCode , Manifest .permission .WRITE_EXTERNAL_STORAGE );
267273 } else {
268274 PermissionHelper .requestPermission (this , req .requestCode , Manifest .permission .CAMERA );
269275 }
@@ -517,11 +523,11 @@ private JSONObject createErrorObject(int code, String message) {
517523 */
518524 private Cursor queryImgDB (Uri contentStore ) {
519525 return this .cordova .getActivity ().getContentResolver ().query (
520- contentStore ,
521- new String [] { MediaStore .Images .Media ._ID },
522- null ,
523- null ,
524- null );
526+ contentStore ,
527+ new String [] { MediaStore .Images .Media ._ID },
528+ null ,
529+ null ,
530+ null );
525531 }
526532
527533 /**
0 commit comments