Skip to content
Open
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
36 changes: 36 additions & 0 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ public void stopCamera() {
this.cordova.getActivity().finishActivity((sourceType + 1) * 16 + returnType + 1);
}
}

cleanupPendingResultFiles();
}

public void takePicture(int returnType, int encodingType)
Expand Down Expand Up @@ -901,6 +903,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {

}// If cancelled
else if (resultCode == Activity.RESULT_CANCELED) {
cleanupPendingResultFiles();
this.failPicture("No Image Selected");
}

Expand Down Expand Up @@ -930,6 +933,7 @@ else if (srcType == CAMERA) {

// If cancelled
else if (resultCode == Activity.RESULT_CANCELED) {
cleanupPendingResultFiles();
this.failPicture("No Image Selected");
}

Expand All @@ -949,6 +953,7 @@ public void run() {
}
});
} else if (resultCode == Activity.RESULT_CANCELED) {
cleanupPendingResultFiles();
this.failPicture("No Image Selected");
} else {
this.failPicture("Selection did not complete!");
Expand Down Expand Up @@ -1214,6 +1219,37 @@ private void cleanup(Uri oldImage, Uri newImage, Bitmap bitmap) {
System.gc();
}

/**
* Best-effort cleanup for temporary files when capture/selection is cancelled or stopped.
*/
private void cleanupPendingResultFiles() {
deleteIfExists(this.imageUri);
deleteIfExists(this.croppedUri);

if (this.croppedFilePath != null) {
new File(this.croppedFilePath).delete();
this.croppedFilePath = null;
}

this.imageUri = null;
this.croppedUri = null;
}

private void deleteIfExists(Uri uri) {
if (uri == null) {
return;
}

try {
String filePath = FileHelper.stripFileProtocol(uri.toString());
if (filePath != null) {
new File(filePath).delete();
}
} catch (Exception ignored) {
// Best-effort cleanup only.
}
}

/**
* Determine if we are storing the images in internal or external storage
*
Expand Down
Loading