@@ -18,34 +18,13 @@ Licensed to the Apache Software Foundation (ASF) under one
1818*/
1919package org .apache .cordova .camera ;
2020
21- import java .io .ByteArrayOutputStream ;
22- import java .io .File ;
23- import java .io .FileInputStream ;
24- import java .io .FileNotFoundException ;
25- import java .io .FileOutputStream ;
26- import java .io .IOException ;
27- import java .io .InputStream ;
28- import java .io .OutputStream ;
29- import java .text .SimpleDateFormat ;
30- import java .util .Date ;
31-
32- import org .apache .cordova .BuildHelper ;
33- import org .apache .cordova .CallbackContext ;
34- import org .apache .cordova .CordovaPlugin ;
35- import org .apache .cordova .CordovaResourceApi ;
36- import org .apache .cordova .LOG ;
37- import org .apache .cordova .PermissionHelper ;
38- import org .apache .cordova .PluginResult ;
39- import org .json .JSONArray ;
40- import org .json .JSONException ;
41-
4221import android .Manifest ;
43- import android .annotation .TargetApi ;
4422import android .app .Activity ;
4523import android .content .ActivityNotFoundException ;
4624import android .content .ContentValues ;
47- import android .content .Context ;
4825import android .content .Intent ;
26+ import android .content .pm .PackageManager ;
27+ import android .content .pm .PackageManager .NameNotFoundException ;
4928import android .database .Cursor ;
5029import android .graphics .Bitmap ;
5130import android .graphics .Bitmap .CompressFormat ;
@@ -55,16 +34,31 @@ Licensed to the Apache Software Foundation (ASF) under one
5534import android .media .MediaScannerConnection ;
5635import android .media .MediaScannerConnection .MediaScannerConnectionClient ;
5736import android .net .Uri ;
58- import android .os .Build ;
5937import android .os .Bundle ;
6038import android .os .Environment ;
61- import android .provider .DocumentsContract ;
6239import android .provider .MediaStore ;
63- import android .provider .OpenableColumns ;
6440import android .support .v4 .content .FileProvider ;
6541import android .util .Base64 ;
66- import android .content .pm .PackageManager ;
67- import android .content .pm .PackageManager .NameNotFoundException ;
42+
43+ import org .apache .cordova .BuildHelper ;
44+ import org .apache .cordova .CallbackContext ;
45+ import org .apache .cordova .CordovaPlugin ;
46+ import org .apache .cordova .LOG ;
47+ import org .apache .cordova .PermissionHelper ;
48+ import org .apache .cordova .PluginResult ;
49+ import org .json .JSONArray ;
50+ import org .json .JSONException ;
51+
52+ import java .io .ByteArrayOutputStream ;
53+ import java .io .File ;
54+ import java .io .FileInputStream ;
55+ import java .io .FileNotFoundException ;
56+ import java .io .FileOutputStream ;
57+ import java .io .IOException ;
58+ import java .io .InputStream ;
59+ import java .io .OutputStream ;
60+ import java .text .SimpleDateFormat ;
61+ import java .util .Date ;
6862
6963/**
7064 * This class launches the camera view, allows the user to take a picture, closes the camera view,
@@ -87,13 +81,19 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
8781
8882 private static final int JPEG = 0 ; // Take a picture of type JPEG
8983 private static final int PNG = 1 ; // Take a picture of type PNG
90- private static final String JPEG_EXTENSION = ".jpg" ;
91- private static final String PNG_EXTENSION = ".png" ;
84+ private static final String JPEG_TYPE = "jpg" ;
85+ private static final String PNG_TYPE = "png" ;
86+ private static final String JPEG_EXTENSION = "." + JPEG_TYPE ;
87+ private static final String PNG_EXTENSION = "." + PNG_TYPE ;
9288 private static final String PNG_MIME_TYPE = "image/png" ;
9389 private static final String JPEG_MIME_TYPE = "image/jpeg" ;
9490 private static final String GET_PICTURE = "Get Picture" ;
9591 private static final String GET_VIDEO = "Get Video" ;
9692 private static final String GET_All = "Get All" ;
93+ private static final String CROPPED_URI_KEY = "croppedUri" ;
94+ private static final String IMAGE_URI_KEY = "imageUri" ;
95+
96+ private static final String TAKE_PICTURE_ACTION = "takePicture" ;
9797
9898 public static final int PERMISSION_DENIED_ERROR = 20 ;
9999 public static final int TAKE_PIC_SEC = 0 ;
@@ -147,7 +147,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
147147 this .applicationId = preferences .getString ("applicationId" , this .applicationId );
148148
149149
150- if (action .equals ("takePicture" )) {
150+ if (action .equals (TAKE_PICTURE_ACTION )) {
151151 this .srcType = CAMERA ;
152152 this .destType = FILE_URI ;
153153 this .saveToPhotoAlbum = false ;
@@ -312,7 +312,6 @@ public void takePicture(int returnType, int encodingType)
312312 PackageManager mPm = this .cordova .getActivity ().getPackageManager ();
313313 if (intent .resolveActivity (mPm ) != null )
314314 {
315-
316315 this .cordova .startActivityForResult ((CordovaPlugin ) this , intent , (CAMERA + 1 ) * 16 + returnType + 1 );
317316 }
318317 else
@@ -358,7 +357,6 @@ private File createCaptureFile(int encodingType, String fileName) {
358357 }
359358
360359
361-
362360 /**
363361 * Get image from photo library.
364362 *
@@ -640,7 +638,7 @@ private String outputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
640638 // Get filename from uri
641639 String fileName = realPath != null ?
642640 realPath .substring (realPath .lastIndexOf ('/' ) + 1 ) :
643- "modified." + (this .encodingType == JPEG ? "jpg" : "png" );
641+ "modified." + (this .encodingType == JPEG ? JPEG_TYPE : PNG_TYPE );
644642
645643 String timeStamp = new SimpleDateFormat (TIME_FORMAT ).format (new Date ());
646644 //String fileName = "IMG_" + timeStamp + (this.encodingType == JPEG ? ".jpg" : ".png");
@@ -1348,11 +1346,11 @@ public Bundle onSaveInstanceState() {
13481346 state .putBoolean ("saveToPhotoAlbum" , this .saveToPhotoAlbum );
13491347
13501348 if (this .croppedUri != null ) {
1351- state .putString ("croppedUri" , this .croppedUri .toString ());
1349+ state .putString (CROPPED_URI_KEY , this .croppedUri .toString ());
13521350 }
13531351
13541352 if (this .imageUri != null ) {
1355- state .putString ("imageUri" , this .imageUri .getFileUri ().toString ());
1353+ state .putString (IMAGE_URI_KEY , this .imageUri .getFileUri ().toString ());
13561354 }
13571355
13581356 return state ;
@@ -1371,38 +1369,34 @@ public void onRestoreStateForActivityResult(Bundle state, CallbackContext callba
13711369 this .correctOrientation = state .getBoolean ("correctOrientation" );
13721370 this .saveToPhotoAlbum = state .getBoolean ("saveToPhotoAlbum" );
13731371
1374- if (state .containsKey ("croppedUri" )) {
1375- this .croppedUri = Uri .parse (state .getString ("croppedUri" ));
1372+ if (state .containsKey (CROPPED_URI_KEY )) {
1373+ this .croppedUri = Uri .parse (state .getString (CROPPED_URI_KEY ));
13761374 }
13771375
1378- if (state .containsKey ("imageUri" )) {
1376+ if (state .containsKey (IMAGE_URI_KEY )) {
13791377 //I have no idea what type of URI is being passed in
1380- this .imageUri = new CordovaUri (Uri .parse (state .getString ("imageUri" )));
1378+ this .imageUri = new CordovaUri (Uri .parse (state .getString (IMAGE_URI_KEY )));
13811379 }
13821380
13831381 this .callbackContext = callbackContext ;
13841382 }
13851383
1386- /*
1387- * This is dirty, but it does the job.
1388- *
1389- * Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
1390- * and since we actually need the Camera to create the file for us most of the time, we don't
1391- * actually write the file, just generate the location based on a timestamp, we need to get it
1392- * back from the Intent.
1393- *
1394- * However, the FilesProvider preserves the path, so we can at least write to it from here, since
1395- * we own the context in this case.
1396- */
1397-
1384+ /*
1385+ * This is dirty, but it does the job.
1386+ *
1387+ * Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
1388+ * and since we actually need the Camera to create the file for us most of the time, we don't
1389+ * actually write the file, just generate the location based on a timestamp, we need to get it
1390+ * back from the Intent.
1391+ *
1392+ * However, the FilesProvider preserves the path, so we can at least write to it from here, since
1393+ * we own the context in this case.
1394+ */
13981395 private String getFileNameFromUri (Uri uri ) {
13991396 String fullUri = uri .toString ();
14001397 String partial_path = fullUri .split ("external_files" )[1 ];
14011398 File external_storage = Environment .getExternalStorageDirectory ();
14021399 String path = external_storage .getAbsolutePath () + partial_path ;
14031400 return path ;
1404-
14051401 }
1406-
1407-
14081402}
0 commit comments