Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(android): remove query img usage #907

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 2 additions & 61 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private boolean allowEdit; // Should we allow the user to crop the image.

public CallbackContext callbackContext;
private int numPics;

private MediaScannerConnection conn; // Used to update gallery app with newly-written files
private Uri scanMe; // Uri of image to be added to content store
Expand Down Expand Up @@ -307,9 +306,6 @@ public void callTakePicture(int returnType, int encodingType) {

public void takePicture(int returnType, int encodingType)
{
// Save the number of images currently on disk for later
this.numPics = queryImgDB(whichContentStore()).getCount();

// Let's use the intent and see what happens
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Expand All @@ -336,8 +332,6 @@ public void takePicture(int returnType, int encodingType)
LOG.d(LOG_TAG, "Error: You don't have a default camera. Your device may not be CTS complaint.");
}
}
// else
// LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
}

/**
Expand Down Expand Up @@ -559,12 +553,7 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep
return;
}


this.processPicture(bitmap, this.encodingType);

if (!this.saveToPhotoAlbum) {
checkForDuplicateImage(DATA_URL);
}
}

// If sending filename back
Expand Down Expand Up @@ -626,7 +615,7 @@ else if (destType == FILE_URI) {
throw new IllegalStateException();
}

this.cleanup(FILE_URI, this.imageUri, galleryUri, bitmap);
this.cleanup(this.imageUri, galleryUri, bitmap);
bitmap = null;
input.close();
}
Expand Down Expand Up @@ -1207,34 +1196,19 @@ public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth,
}
}

/**
* Creates a cursor that can be used to determine how many images we have.
*
* @return a cursor
*/
private Cursor queryImgDB(Uri contentStore) {
return this.cordova.getActivity().getContentResolver().query(
contentStore,
new String[]{MediaStore.Images.Media._ID},
null,
null,
null);
}

/**
* Cleans up after picture taking. Checking for duplicates and that kind of stuff.
*
* @param newImage
*/
private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) {
private void cleanup(Uri oldImage, Uri newImage, Bitmap bitmap) {
if (bitmap != null) {
bitmap.recycle();
}

// 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);
Expand All @@ -1243,37 +1217,6 @@ private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) {
System.gc();
}

/**
* 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 = 1;
Uri contentStore = whichContentStore();
Cursor cursor = queryImgDB(contentStore);
int currentNumOfImages = cursor.getCount();

if (type == FILE_URI && this.saveToPhotoAlbum) {
diff = 2;
}

// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) == diff) {
cursor.moveToLast();
@SuppressLint("Range")
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
if (diff == 2) {
id--;
}
Uri uri = Uri.parse(contentStore + "/" + id);
this.cordova.getActivity().getContentResolver().delete(uri, null, null);
cursor.close();
}
}

/**
* Determine if we are storing the images in internal or external storage
*
Expand Down Expand Up @@ -1377,7 +1320,6 @@ public Bundle onSaveInstanceState() {
state.putInt("targetHeight", this.targetHeight);
state.putInt("encodingType", this.encodingType);
state.putInt("mediaType", this.mediaType);
state.putInt("numPics", this.numPics);
state.putBoolean("allowEdit", this.allowEdit);
state.putBoolean("correctOrientation", this.correctOrientation);
state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum);
Expand All @@ -1401,7 +1343,6 @@ public void onRestoreStateForActivityResult(Bundle state, CallbackContext callba
this.targetHeight = state.getInt("targetHeight");
this.encodingType = state.getInt("encodingType");
this.mediaType = state.getInt("mediaType");
this.numPics = state.getInt("numPics");
this.allowEdit = state.getBoolean("allowEdit");
this.correctOrientation = state.getBoolean("correctOrientation");
this.saveToPhotoAlbum = state.getBoolean("saveToPhotoAlbum");
Expand Down
Loading