Updating CameraLauncher based on changes in master, read pre-3.0 commit history for author info

This commit is contained in:
Joe Bowser 2013-05-13 11:42:53 -07:00
parent aaef443dc6
commit 5455bbeb37

View File

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