From 367d7500d582e406e5f2a70bfefd9e102e51d4e0 Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 29 Sep 2011 22:29:35 -0400 Subject: [PATCH 1/4] Deletes any duplicate images taken by camera --- .../src/com/phonegap/CameraLauncher.java | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index f5d3b4e5..19798c6c 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -24,9 +24,12 @@ import com.phonegap.api.PluginResult; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.net.Uri; +import android.provider.ContactsContract; +import android.provider.MediaStore; import android.util.Log; /** @@ -63,6 +66,7 @@ public class CameraLauncher extends Plugin { private int mediaType; // What type of media to retrieve public String callbackId; + private int numPics; /** * Constructor. @@ -147,7 +151,10 @@ public class CameraLauncher extends Plugin { */ public void takePicture(int quality, int returnType, int encodingType) { this.mQuality = quality; - + + // Save the number of images currently on disk for later + this.numPics = queryImgDB().getCount(); + // Display camera Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); @@ -266,7 +273,7 @@ public class CameraLauncher extends Plugin { // Get src and dest types from request code int srcType = (requestCode/16) - 1; int destType = (requestCode % 16) - 1; - + // If CAMERA if (srcType == CAMERA) { // If image available @@ -294,6 +301,7 @@ public class CameraLauncher extends Plugin { // If sending base64 image back if (destType == DATA_URL) { this.processPicture(bitmap); + checkForDuplicateImage(DATA_URL); } // If sending filename back @@ -333,6 +341,8 @@ public class CameraLauncher extends Plugin { bitmap.recycle(); bitmap = null; System.gc(); + + checkForDuplicateImage(FILE_URI); } catch (IOException e) { e.printStackTrace(); this.failPicture("Error capturing image."); @@ -414,7 +424,49 @@ public class CameraLauncher extends Plugin { } } } + + /** + * Creates a cursor that can be used to determine how many images we have. + * + * @return a cursor + */ + private Cursor queryImgDB() { + return this.ctx.getContentResolver().query( + android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + new String[] { MediaStore.Images.Media._ID }, + null, + null, + null); + } + /** + * Used to find out if we are in a situation where the Camera Intent adds to images + * to the content store. If we are using a FILE_URI and the number of images in the DB + * increases by 2 we have a duplicate, when using a DATA_URL the number is 1. + * + * @param type FILE_URI or DATA_URL + */ + private void checkForDuplicateImage(int type) { + int diff = 0; + Cursor cursor = queryImgDB(); + int currentNumOfImages = cursor.getCount(); + + if (type == FILE_URI) { + diff = 1; + } + + // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL + if ((currentNumOfImages - numPics) > diff) { + cursor.moveToLast(); + int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))) - 1; + Uri uri = Uri.parse(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + id); + + Log.d(LOG_TAG, "Deleting URI = " + uri.toString()); + + this.ctx.getContentResolver().delete(uri, null, null); + } + } + /** * Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript. * From 6098f46d0835664bc5343a277877885ea95a5ca6 Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 29 Sep 2011 23:09:44 -0400 Subject: [PATCH 2/4] When you use the File API to remove a file need to check to see if we need to delete a row from the content store --- framework/src/com/phonegap/FileUtils.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index 29b1dd3c..17a94780 100755 --- a/framework/src/com/phonegap/FileUtils.java +++ b/framework/src/com/phonegap/FileUtils.java @@ -158,6 +158,7 @@ public class FileUtils extends Plugin { success = remove(args.getString(0)); if (success) { + notifyDelete(args.getString(0)); return new PluginResult(status); } else { JSONObject error = new JSONObject().put("code", FileUtils.NO_MODIFICATION_ALLOWED_ERR); @@ -221,6 +222,17 @@ public class FileUtils extends Plugin { } /** + * Need to check to see if we need to clean up the content store + * + * @param filePath the path to check + */ + private void notifyDelete(String filePath) { + int result = this.ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + MediaStore.Images.Media.DATA + " = ?", + new String[] {filePath}); + } + + /** * Allows the user to look up the Entry for a file or directory referred to by a local URI. * * @param url of the file/directory to look up From 80695ec5e43073afc986074a3301657b1b29e905 Mon Sep 17 00:00:00 2001 From: macdonst Date: Fri, 30 Sep 2011 11:30:04 -0400 Subject: [PATCH 3/4] Fix issue with DATA_URL and refactor code --- framework/assets/js/camera.js | 56 ++++++++----------- .../src/com/phonegap/CameraLauncher.java | 55 +++++++----------- 2 files changed, 44 insertions(+), 67 deletions(-) diff --git a/framework/assets/js/camera.js b/framework/assets/js/camera.js index 88e7a475..00f8557e 100755 --- a/framework/assets/js/camera.js +++ b/framework/assets/js/camera.js @@ -105,56 +105,46 @@ Camera.prototype.getPicture = function(successCallback, errorCallback, options) console.log("Camera Error: errorCallback is not a function"); return; } - - this.options = options; - var quality = 80; - if (options.quality) { - quality = this.options.quality; - } - var maxResolution = 0; - if (options.maxResolution) { - maxResolution = this.options.maxResolution; + if (!options) { + options = {}; } - - var destinationType = Camera.DestinationType.DATA_URL; - if (this.options.destinationType) { - destinationType = this.options.destinationType; + if (!options.quality) { + options.quality = 80; } - var sourceType = Camera.PictureSourceType.CAMERA; - if (typeof this.options.sourceType === "number") { - sourceType = this.options.sourceType; + if (!options.maxResolution) { + options.maxResolution = 0; } - var encodingType = Camera.EncodingType.JPEG; - if (typeof options.encodingType == "number") { - encodingType = this.options.encodingType; + if (!options.destinationType) { + options.destinationType = Camera.DestinationType.DATA_URL; } - var mediaType = Camera.MediaType.PICTURE; - if (typeof options.mediaType == "number") { - mediaType = this.options.mediaType; + if (!options.sourceType) { + options.sourceType = Camera.PictureSourceType.CAMERA; } - - var targetWidth = -1; - if (typeof options.targetWidth == "number") { - targetWidth = options.targetWidth; + if (!options.encodingType) { + options.encodingType = Camera.EncodingType.JPEG; + } + if (!options.mediaType) { + options.mediaType = Camera.MediaType.PICTURE; + } + if (!options.targetWidth) { + options.targetWidth = -1; } else if (typeof options.targetWidth == "string") { var width = new Number(options.targetWidth); if (isNaN(width) === false) { - targetWidth = width.valueOf(); + options.targetWidth = width.valueOf(); } } - - var targetHeight = -1; - if (typeof options.targetHeight == "number") { - targetHeight = options.targetHeight; + if (!options.targetHeight) { + options.targetHeight = -1; } else if (typeof options.targetHeight == "string") { var height = new Number(options.targetHeight); if (isNaN(height) === false) { - targetHeight = height.valueOf(); + options.targetHeight = height.valueOf(); } } - PhoneGap.exec(successCallback, errorCallback, "Camera", "takePicture", [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType, mediaType]); + PhoneGap.exec(successCallback, errorCallback, "Camera", "takePicture", [options]); }; PhoneGap.addConstructor(function() { diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index 19798c6c..e885bb17 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -17,6 +17,7 @@ import java.io.OutputStream; import org.apache.commons.codec.binary.Base64; import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; @@ -28,7 +29,6 @@ import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.net.Uri; -import android.provider.ContactsContract; import android.provider.MediaStore; import android.util.Log; @@ -89,36 +89,30 @@ public class CameraLauncher extends Plugin { try { if (action.equals("takePicture")) { + int srcType = CAMERA; int destType = DATA_URL; this.targetHeight = 0; this.targetWidth = 0; - - if (args.length() > 1) { - destType = args.getInt(1); - } - int srcType = CAMERA; - if (args.length() > 2) { - srcType = args.getInt(2); - } - if (args.length() > 3) { - this.targetWidth = args.getInt(3); - } - if (args.length() > 4) { - this.targetHeight = args.getInt(4); - } this.encodingType = JPEG; - if (args.length() > 5) { - this.encodingType = args.getInt(5); - } this.mediaType = PICTURE; - if (args.length() > 6) { - this.mediaType = args.getInt(6); + this.mQuality = 80; + + JSONObject options = args.optJSONObject(0); + if (options != null) { + srcType = options.getInt("sourceType"); + destType = options.getInt("destinationType"); + this.targetHeight = options.getInt("targetHeight"); + this.targetWidth = options.getInt("targetWidth"); + this.encodingType = options.getInt("encodingType"); + this.mediaType = options.getInt("mediaType"); + this.mQuality = options.getInt("quality"); } + if (srcType == CAMERA) { - this.takePicture(args.getInt(0), destType, encodingType); + this.takePicture(destType, encodingType); } else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) { - this.getImage(args.getInt(0), srcType, destType); + this.getImage(srcType, destType); } PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); @@ -149,9 +143,7 @@ public class CameraLauncher extends Plugin { * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality) * @param returnType Set the type of image to return. */ - public void takePicture(int quality, int returnType, int encodingType) { - this.mQuality = quality; - + public void takePicture(int returnType, int encodingType) { // Save the number of images currently on disk for later this.numPics = queryImgDB().getCount(); @@ -191,9 +183,7 @@ public class CameraLauncher extends Plugin { * @param returnType Set the type of image to return. */ // TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do! - public void getImage(int quality, int srcType, int returnType) { - this.mQuality = quality; - + public void getImage(int srcType, int returnType) { Intent intent = new Intent(); String title = GET_PICTURE; if (this.mediaType == PICTURE) { @@ -447,22 +437,19 @@ public class CameraLauncher extends Plugin { * @param type FILE_URI or DATA_URL */ private void checkForDuplicateImage(int type) { - int diff = 0; + int diff = 1; Cursor cursor = queryImgDB(); int currentNumOfImages = cursor.getCount(); if (type == FILE_URI) { - diff = 1; + diff = 2; } // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL - if ((currentNumOfImages - numPics) > diff) { + if ((currentNumOfImages - numPics) == diff) { cursor.moveToLast(); int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))) - 1; Uri uri = Uri.parse(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + id); - - Log.d(LOG_TAG, "Deleting URI = " + uri.toString()); - this.ctx.getContentResolver().delete(uri, null, null); } } From 24ad506da587345c81e5790d50992d095baef083 Mon Sep 17 00:00:00 2001 From: macdonst Date: Fri, 30 Sep 2011 14:26:48 -0400 Subject: [PATCH 4/4] Fixing JavaScript comparrison issues --- framework/assets/js/camera.js | 24 +++++++++++++----------- framework/assets/js/filetransfer.js | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/framework/assets/js/camera.js b/framework/assets/js/camera.js index 00f8557e..d4fbad4e 100755 --- a/framework/assets/js/camera.js +++ b/framework/assets/js/camera.js @@ -106,38 +106,40 @@ Camera.prototype.getPicture = function(successCallback, errorCallback, options) return; } - if (!options) { + if (options === null || typeof options === "undefined") { options = {}; } - if (!options.quality) { + if (options.quality === null || typeof options.quality === "undefined") { options.quality = 80; } - if (!options.maxResolution) { + if (options.maxResolution === null || typeof options.maxResolution === "undefined") { options.maxResolution = 0; } - if (!options.destinationType) { + if (options.destinationType === null || typeof options.destinationType === "undefined") { options.destinationType = Camera.DestinationType.DATA_URL; } - if (!options.sourceType) { + if (options.sourceType === null || typeof options.sourceType === "undefined") { options.sourceType = Camera.PictureSourceType.CAMERA; } - if (!options.encodingType) { + if (options.encodingType === null || typeof options.encodingType === "undefined") { options.encodingType = Camera.EncodingType.JPEG; } - if (!options.mediaType) { + if (options.mediaType === null || typeof options.mediaType === "undefined") { options.mediaType = Camera.MediaType.PICTURE; } - if (!options.targetWidth) { + if (options.targetWidth === null || typeof options.targetWidth === "undefined") { options.targetWidth = -1; - } else if (typeof options.targetWidth == "string") { + } + else if (typeof options.targetWidth == "string") { var width = new Number(options.targetWidth); if (isNaN(width) === false) { options.targetWidth = width.valueOf(); } } - if (!options.targetHeight) { + if (options.targetHeight === null || typeof options.targetHeight === "undefined") { options.targetHeight = -1; - } else if (typeof options.targetHeight == "string") { + } + else if (typeof options.targetHeight == "string") { var height = new Number(options.targetHeight); if (isNaN(height) === false) { options.targetHeight = height.valueOf(); diff --git a/framework/assets/js/filetransfer.js b/framework/assets/js/filetransfer.js index 5db046ae..adab0634 100644 --- a/framework/assets/js/filetransfer.js +++ b/framework/assets/js/filetransfer.js @@ -58,7 +58,7 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro fileKey = options.fileKey; fileName = options.fileName; mimeType = options.mimeType; - if (options.chunkedMode) { + if (options.chunkedMode != null || typeof options.chunkedMode != "undefined") { chunkedMode = options.chunkedMode; } if (options.params) {