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:
Your best bet is probably to handle the Camera class directly, and then store the jpeg or raw returned in the callback where you want.
Alternatively, you could try and delete the taken image by _id from the content provider for media after it's been added. Just find it by query and delete it via the ContentResolver.delete, not sure about the implementation though.
This is the code I use which takes a pic and saves it at the location specified
I then handle the response.
I had to declare the outputFileUri as a global variable as I found no way of acquiring the saved path onActivityResult. By passing the outputFileUri you will notice that the image isn't saved in the Camera folde but at the location specified. I've tried this code on my Nexus1 and a cheap samsung thing.
Hope this helps
After struggeling with this for some time, I bit the bullet and wrote my own camera caputere activity. I am convinced this is much more portable and safer than the
MediaStore.ACTION_IMAGE_CAPTURE
solutions. The image will be stored where you store it and nowhere else and you are not in danger to delete some unrelated file by accident. Also, it is possible to adapt the actual camera function exactly to the requirements.For portability reasons, I used the
Camera
class, not camera2.It is worth to note that the camera parameters should all be set, especially picture size, focus mode and flash mode, they all may be in an unexpected state when starting the camera.
Posting this as an answer, not as a comment, because in my opinion it is the correct answer resulting in minimal effort.
This is not an easy task to solve but can approach a tricky method.If any one need a simple and working solution for this. Try the below code: