I know this has been asked in many different ways but I still can not seem to delete the gallery image from the default folder. I’m saving the file to the SD card correctly and I can delete that file fine, but the default gallery image file that shows under the folder Camera will not delete.
I would like the image to delete once the activity is returned since the file is already stored on the SD card under /Coupon2.
Any suggestions?
public void startCamera() {
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
mManufacturerText = (EditText) findViewById(R.id.manufacturer);
String ManufacturerText = mManufacturerText.getText().toString();
String currentDateTimeString = new Date().toString();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File filedir = new File(Environment.getExternalStorageDirectory()+"/Coupon2");
filedir.mkdirs();
File file = new File(Environment.getExternalStorageDirectory()+"/Coupon2", ManufacturerText+"-test.png");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == -1) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
intent.putExtra("return-data", false);
intent.setDataAndType(outputFileUri, "image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_CROP_REQUEST);
}else {
SetImage();
saveState();
}
}
My application requires me to call an intent to take a photo. The photo cannot be in the gallery but instead must be in a specific directory on the SD card.
Originally I just used the EXTRA_OUTPUT, but I soon discovered the following:
– Some devices use it completely and skip the gallery.
– Some devices ignore it completely and ONLY use the gallery.
– Some devices really suck and save a full sized image to the gallery, and save a thumbnail only to the location I wanted. (HTC you know who you are…)
So, I can’t blindly delete a gallery file when I’m done. The last added photo may or may not be the one I want to remove. Also, I may have to copy that file replacing my own afterwards. Because my activity is 2000 lines, and my company wouldn’t want all of our code being posted, I’m posting only the methods involved in doing this. Hopefully this helps.
Also, I’ll state, this is my first Android application. I wouldn’t be surprised if there was a better way to do this that I just don’t know about, but this is what’s working for me!
So, here is my solution:
First, in my application context I define a variable as follows:
Next, in my activity, I define a method to get a list of all photos in the gallery:
Here’s a method to return a unique file name for my new image:
I have three variables in my Activity that store information for me about a current file. A string (path), a File variable, and a URI to that file:
I never set these directly, I only call a setter on the file path:
Now I call an intent to take a photo.
Once this is done, and the activity comes back, here is my code: