diff --git a/plugin.xml b/plugin.xml index 7162678..1f7db02 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,21 +5,20 @@ xmlns:android="http://schemas.android.com/apk/res/android" id="org.apache.cordova.core.CameraLauncher" version="0.1.0"> Camera - - - - - + - + - + - + + + + @@ -40,10 +39,8 @@ id="org.apache.cordova.core.CameraLauncher" - diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 86fab9c..0d17277 100755 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -20,18 +20,17 @@ 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; +import org.apache.cordova.ExifHelper; 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; @@ -45,11 +44,13 @@ 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; import android.os.Environment; import android.provider.MediaStore; +import android.util.Base64; import android.util.Log; /** @@ -292,7 +293,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect // If sending base64 image back if (destType == DATA_URL) { - bitmap = getScaledBitmap(imageUri.toString()); + bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString())); if (bitmap == null) { // Try to get the bitmap from intent. bitmap = (Bitmap)intent.getExtras().get("data"); @@ -318,9 +319,7 @@ 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 - DataResource dataResource = DataResource.initiateNewDataRequestForUri(inputUri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent"); - File file = dataResource.getRealFile(); - uri = Uri.fromFile(file); + uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova))); } else { uri = Uri.fromFile(new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), System.currentTimeMillis() + ".jpg")); } @@ -336,15 +335,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect this.callbackContext.success(uri.toString()); } else { - bitmap = getScaledBitmap(imageUri.toString()); + bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString())); if (rotate != 0 && this.correctOrientation) { bitmap = getRotatedBitmap(rotate, bitmap, exif); } // Add compressed version of captured image to returned media store Uri - DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent"); - OutputStream os = dataResource.getOutputStream(); + OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); os.close(); @@ -352,7 +350,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect if (this.encodingType == JPEG) { String exifPath; if (this.saveToPhotoAlbum) { - exifPath = dataResource.getRealFile().getPath(); + exifPath = FileHelper.getRealPath(uri, this.cordova); } else { exifPath = uri.getPath(); } @@ -403,9 +401,8 @@ 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 = dataResource.getMimeType(); + String mimeType = FileHelper.getMimeType(uriString, this.cordova); // 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"); @@ -446,8 +443,7 @@ 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). - File realFile = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent").getRealFile(); - String realPath = realFile != null? realFile.getPath() : null; + String realPath = FileHelper.getRealPath(uri, this.cordova); ExifHelper exif = new ExifHelper(); if (realPath != null && this.encodingType == JPEG) { try { @@ -541,15 +537,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect */ private void writeUncompressedImage(Uri uri) throws FileNotFoundException, IOException { - 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"); - } + FileInputStream fis = new FileInputStream(FileHelper.stripFileProtocol(imageUri.toString())); + OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri); byte[] buffer = new byte[4096]; int len; while ((len = fis.read(buffer)) != -1) { @@ -592,15 +581,14 @@ 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(dataResource.getInputStream()); + return BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova)); } // figure out the original width and height of the image BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeStream(dataResource.getInputStream(), null, options); + BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options); //CB-2292: WTF? Why is the width null? if(options.outWidth == 0 || options.outHeight == 0) @@ -614,7 +602,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(dataResource.getInputStream(), null, options); + Bitmap unscaledBitmap = BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options); if (unscaledBitmap == null) { return null; } @@ -713,20 +701,16 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect bitmap.recycle(); } - 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(); + // Clean up initial camera-written image file. + (new File(FileHelper.stripFileProtocol(oldImage.toString()))).delete(); - checkForDuplicateImage(imageType); - // Scan for the gallery to update pic refs in gallery - if (this.saveToPhotoAlbum && newImage != null) { - this.scanForGallery(newImage); - } - - System.gc(); + checkForDuplicateImage(imageType); + // Scan for the gallery to update pic refs in gallery + if (this.saveToPhotoAlbum && newImage != null) { + this.scanForGallery(newImage); } + + System.gc(); } /** @@ -780,7 +764,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect try { if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) { byte[] code = jpeg_data.toByteArray(); - byte[] output = Base64.encodeBase64(code); + byte[] output = Base64.encode(code, Base64.DEFAULT); String js_out = new String(output); this.callbackContext.success(js_out); js_out = null;