forked from github/cordova-android
Deletes any duplicate images taken by camera
This commit is contained in:
parent
0d57404cf1
commit
367d7500d5
@ -24,9 +24,12 @@ import com.phonegap.api.PluginResult;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Bitmap.CompressFormat;
|
import android.graphics.Bitmap.CompressFormat;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.provider.ContactsContract;
|
||||||
|
import android.provider.MediaStore;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,6 +66,7 @@ public class CameraLauncher extends Plugin {
|
|||||||
private int mediaType; // What type of media to retrieve
|
private int mediaType; // What type of media to retrieve
|
||||||
|
|
||||||
public String callbackId;
|
public String callbackId;
|
||||||
|
private int numPics;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -148,6 +152,9 @@ public class CameraLauncher extends Plugin {
|
|||||||
public void takePicture(int quality, int returnType, int encodingType) {
|
public void takePicture(int quality, int returnType, int encodingType) {
|
||||||
this.mQuality = quality;
|
this.mQuality = quality;
|
||||||
|
|
||||||
|
// Save the number of images currently on disk for later
|
||||||
|
this.numPics = queryImgDB().getCount();
|
||||||
|
|
||||||
// Display camera
|
// Display camera
|
||||||
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
|
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
|
||||||
|
|
||||||
@ -294,6 +301,7 @@ public class CameraLauncher extends Plugin {
|
|||||||
// If sending base64 image back
|
// If sending base64 image back
|
||||||
if (destType == DATA_URL) {
|
if (destType == DATA_URL) {
|
||||||
this.processPicture(bitmap);
|
this.processPicture(bitmap);
|
||||||
|
checkForDuplicateImage(DATA_URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If sending filename back
|
// If sending filename back
|
||||||
@ -333,6 +341,8 @@ public class CameraLauncher extends Plugin {
|
|||||||
bitmap.recycle();
|
bitmap.recycle();
|
||||||
bitmap = null;
|
bitmap = null;
|
||||||
System.gc();
|
System.gc();
|
||||||
|
|
||||||
|
checkForDuplicateImage(FILE_URI);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
this.failPicture("Error capturing image.");
|
this.failPicture("Error capturing image.");
|
||||||
@ -415,6 +425,48 @@ 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.
|
* Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user