From 44475d9df98f7e004a529f1e0d8851bf786788a7 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Thu, 1 Oct 2015 16:19:06 -0700 Subject: [PATCH] Using the CordovaResourceApi to fine paths of files in the background thread. If the file doesn't exist, return the content URI We also do a refactor to bring this in line. This code got bike-shedded a bit. --- src/android/CameraLauncher.java | 54 ++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 28e96bc..8a7f9e7 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -32,6 +32,7 @@ import java.util.Date; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaResourceApi; import org.apache.cordova.LOG; import org.apache.cordova.PluginResult; import org.json.JSONArray; @@ -104,8 +105,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 }; public CallbackContext callbackContext; private int numPics; @@ -115,28 +115,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect private Uri croppedUri; - /** - * This plugin requires read access to the storage. - */ - - protected int checkReadStorage() - { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) - { - return cordova.getActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE); - } - else - { - return PackageManager.PERMISSION_GRANTED; - } - } - protected void getReadPermission(int requestCode) { cordova.requestPermission(this, requestCode, Manifest.permission.READ_EXTERNAL_STORAGE); } - /** * Executes the request and returns PluginResult. * @@ -241,7 +224,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect * @param returnType Set the type of image to return. */ public void callTakePicture(int returnType, int encodingType) { - if (checkReadStorage() == PackageManager.PERMISSION_GRANTED) { + if (cordova.hasPermission(permissions[0])) { takePicture(returnType, encodingType); } else { getReadPermission(TAKE_PIC_SEC); @@ -589,6 +572,21 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { return modifiedPath; } + + + /** + * We already have shared code to resolve URIs, don't need any more. + */ + private String getRealFileFromUri(Uri uri) + { + CordovaResourceApi api = webView.getResourceApi(); + File f = api.mapUriToFile(uri); + if(f != null) + return "file://" + f.getAbsolutePath(); + else + return uri.toString(); + } + /** * Applies all needed transformation to the image received from the gallery. * @@ -607,10 +605,13 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { } int rotate = 0; + String fileLocation = getRealFileFromUri(uri); + Log.d(LOG_TAG, "File locaton is: " + fileLocation); + // If you ask for video or all media type you will automatically get back a file URI // and there will be no attempt to resize any returned data if (this.mediaType != PICTURE) { - this.callbackContext.success(uri.toString()); + this.callbackContext.success(fileLocation); } else { // This is a special case to just return the path as no scaling, @@ -675,7 +676,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { } } else { - this.callbackContext.success(uri.toString()); + this.callbackContext.success(fileLocation); } } if (bitmap != null) { @@ -757,7 +758,13 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { // If retrieving photo from library else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) { if (resultCode == Activity.RESULT_OK && intent != null) { - this.processResultFromGallery(destType, intent); + final Intent i = intent; + final int finalDestType = destType; + cordova.getThreadPool().execute(new Runnable() { + public void run() { + processResultFromGallery(finalDestType, i); + } + }); } else if (resultCode == Activity.RESULT_CANCELED) { this.failPicture("Selection cancelled."); @@ -1177,7 +1184,6 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { break; case SAVE_TO_ALBUM_SEC: break; - } } }