I am using native Android camera and save file to my application data folder (/mnt/sdcard/Android/data/com.company.app/files/Pictures/). At the same time anther copy of photo is saved to DCIM folder.
This is my code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String formattedImageName = getDateString() + ".jpg";
File image_file = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES), formattedImageName);
Uri imageUri = Uri.fromFile(image_file);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, REQUEST_FROM_CAMERA);
How can I prevent saving additional copy of image to DCIM folder?
Many Thanks
You can use the following : First we get the last saved image by checking which was the last modified image. Then check if last modified time is in the last few seconds. You may also have to check the exact location of where camera stores the image.
Unfortunately, some smart phones save images in another folder such as DCIM/100MEDIA. So can't rely to these solution. I prefer use this way:
I tried to find out if a second copy exists and delete the copy. I used the above code to find the last taken picture.
Notice: Don't use
cursor.close();
after using managedQuery, Leave the cursor for the Android system to manage and don't call that. You can see managedQuery()Notice2: The managedQuery method is deprecated and it should be avoided, implement CursorLoaders instead.
A nice solution by Parth. But it's good for Samsungs that keep images in DCIM/Camera. Some phones - Sony Ericssons, HTCs keep them in folders like DCIM/100MEDIA, DCIM/100ANDRO so I have slightly modified the code:
check this code..
and this is where the method is doing all magic
I am encountering a similar problem with the Moto Z Force (7.1.1). I have the
MediaStore.EXTRA_OUTPUT
defined on the intent, but a duplicate file is still created in the camera directory.I need to test on other devices, but here's an approach I took regarding this issue. Rather than trying to find the specific camera directory, I'm using the
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
location.Here's my code snippet: