diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 0aa922b..86fab9c 100755 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -20,10 +20,10 @@ package org.apache.cordova.core; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.codec.binary.Base64; @@ -31,6 +31,7 @@ import org.apache.cordova.DirectoryManager; import org.apache.cordova.FileHelper; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; +import org.apache.cordova.api.DataResource; import org.apache.cordova.api.LOG; import org.apache.cordova.api.PluginResult; import org.json.JSONArray; @@ -44,7 +45,6 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.Bitmap.CompressFormat; -import android.graphics.Rect; import android.media.MediaScannerConnection; import android.media.MediaScannerConnection.MediaScannerConnectionClient; import android.net.Uri; @@ -292,7 +292,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect // If sending base64 image back if (destType == DATA_URL) { - bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString())); + bitmap = getScaledBitmap(imageUri.toString()); if (bitmap == null) { // Try to get the bitmap from intent. bitmap = (Bitmap)intent.getExtras().get("data"); @@ -318,7 +318,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect if (this.saveToPhotoAlbum) { Uri inputUri = getUriFromMediaStore(); //Just because we have a media URI doesn't mean we have a real file, we need to make it - uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova))); + DataResource dataResource = DataResource.initiateNewDataRequestForUri(inputUri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent"); + File file = dataResource.getRealFile(); + uri = Uri.fromFile(file); } else { uri = Uri.fromFile(new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), System.currentTimeMillis() + ".jpg")); } @@ -334,14 +336,15 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect this.callbackContext.success(uri.toString()); } else { - bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString())); + bitmap = getScaledBitmap(imageUri.toString()); if (rotate != 0 && this.correctOrientation) { bitmap = getRotatedBitmap(rotate, bitmap, exif); } // Add compressed version of captured image to returned media store Uri - OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri); + DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent"); + OutputStream os = dataResource.getOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); os.close(); @@ -349,7 +352,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect if (this.encodingType == JPEG) { String exifPath; if (this.saveToPhotoAlbum) { - exifPath = FileHelper.getRealPath(uri, this.cordova); + exifPath = dataResource.getRealFile().getPath(); } else { exifPath = uri.getPath(); } @@ -400,8 +403,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect this.callbackContext.success(uri.toString()); } else { String uriString = uri.toString(); + DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent"); // Get the path to the image. Makes loading so much easier. - String mimeType = FileHelper.getMimeType(uriString, this.cordova); + String mimeType = dataResource.getMimeType(); // If we don't have a valid image so quit. if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) { Log.d(LOG_TAG, "I either have a null image path or bitmap"); @@ -442,7 +446,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect // Create an ExifHelper to save the exif data that is lost during compression String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/resize.jpg"; // Some content: URIs do not map to file paths (e.g. picasa). - String realPath = FileHelper.getRealPath(uri, this.cordova); + File realFile = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent").getRealFile(); + String realPath = realFile != null? realFile.getPath() : null; ExifHelper exif = new ExifHelper(); if (realPath != null && this.encodingType == JPEG) { try { @@ -536,8 +541,15 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect */ private void writeUncompressedImage(Uri uri) throws FileNotFoundException, IOException { - FileInputStream fis = new FileInputStream(FileHelper.stripFileProtocol(imageUri.toString())); - OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri); + DataResource inputDataResource = DataResource.initiateNewDataRequestForUri(imageUri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage"); + InputStream fis = inputDataResource.getInputStream(); + DataResource outDataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage"); + OutputStream os = outDataResource.getOutputStream(); + if(fis == null) { + throw new FileNotFoundException("Could not get the input file"); + } else if(os == null) { + throw new FileNotFoundException("Could not get the output file"); + } byte[] buffer = new byte[4096]; int len; while ((len = fis.read(buffer)) != -1) { @@ -580,14 +592,15 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect */ private Bitmap getScaledBitmap(String imageUrl) throws IOException { // If no new width or height were specified return the original bitmap + DataResource dataResource = DataResource.initiateNewDataRequestForUri(imageUrl, webView.pluginManager, cordova, "CameraLauncher.getScaledBitmap"); if (this.targetWidth <= 0 && this.targetHeight <= 0) { - return BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova)); + return BitmapFactory.decodeStream(dataResource.getInputStream()); } // figure out the original width and height of the image BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options); + BitmapFactory.decodeStream(dataResource.getInputStream(), null, options); //CB-2292: WTF? Why is the width null? if(options.outWidth == 0 || options.outHeight == 0) @@ -601,7 +614,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect // Load in the smallest bitmap possible that is closest to the size we want options.inJustDecodeBounds = false; options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight); - Bitmap unscaledBitmap = BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options); + Bitmap unscaledBitmap = BitmapFactory.decodeStream(dataResource.getInputStream(), null, options); if (unscaledBitmap == null) { return null; } @@ -700,16 +713,20 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect bitmap.recycle(); } - // Clean up initial camera-written image file. - (new File(FileHelper.stripFileProtocol(oldImage.toString()))).delete(); + DataResource dataResource = DataResource.initiateNewDataRequestForUri(oldImage, webView.pluginManager, cordova, "CameraLauncher.cleanup"); + File file = dataResource.getRealFile(); + if(file != null) { + // Clean up initial camera-written image file. + file.delete(); - checkForDuplicateImage(imageType); - // Scan for the gallery to update pic refs in gallery - if (this.saveToPhotoAlbum && newImage != null) { - this.scanForGallery(newImage); + checkForDuplicateImage(imageType); + // Scan for the gallery to update pic refs in gallery + if (this.saveToPhotoAlbum && newImage != null) { + this.scanForGallery(newImage); + } + + System.gc(); } - - System.gc(); } /**