Android: Duplicate photo storage in DCIM folder

纵然是瞬间 提交于 2019-12-01 11:55:19

问题


I am using the native Android camera and save files to a application data folder (/mnt/sdcard/Pictures/). At the same time - on some devices - another copy of photo is saved to DCIM folder.

This is my code:

private void startStockCameraForResult()
{
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    mNextImageFileUri = ImageFileUtils.getOutputMediaFileUri();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mNextImageFileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

How can I prevent the saved additional copy of the image in the DCIM folder?

My Problem is that code produces
1 photo : Samsung Galaxy SIII, Huawei HUAWEI P2-6011 etc.
2 photos : Samsung Galaxy SI, Htc HTC One XL etc.

Other threads describe deleting last added image to DCIM. Problems here are devices which have no problem like Galaxy SIII and Imagename on DCIM and on application data folder is different.

Many Thanks


回答1:


AFAIK, you can't reliably tell the camera apps (device-independently) a) where to save the image AND b) also to save it only once. I had to resort to this approach:

1) Just let the camera app save the picture to wherever it likes, by removing the putExtra(...) statement:

`intent.putExtra(MediaStore.EXTRA_OUTPUT, mNextImageFileUri); // set the image file name`

This (i.e. not setting EXTRA_OUTPUT) will guarantee that only one image will be saved, to the default picture location.

2) Find the last photo taken, and save its ID, for a later safety check. (Query for the last image ID, sorting by DATE_TAKEN.)

3) Fire the capture intent, and in your onActivityResult, again, get the last image taken, and save its ID, URI and path.

4) If your new pic ID is > than the one previously saved, go ahead, otherwise panic out...

5) Move the original image file (using its path) to your preferred location. Now, the original file is removed.

6) Delete the original media image entry, using its URI. Now the image is removed from the gallery, too.

7) If you also want to remove the thumbnails, well, you'll need to query and delete them similarly, but I'd advise against it: a device reboot or another media scan should refresh the thumbnail cache. Also, you may actually quite likely need that thumbnail for a short while after deleting the original image. (If you need it longer, you need be careful: if you moved the photos to the private app dir (getExternalFilesDir(Environment.DIRECTORY_PICTURES)) the media manager will not (be able to) generate thumbnails for you, so you may need to manage your own thumbnails.)



来源:https://stackoverflow.com/questions/19940270/android-duplicate-photo-storage-in-dcim-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!