Android: Duplicate photo storage in DCIM folder

后端 未结 1 938
滥情空心
滥情空心 2021-01-16 15:49

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

相关标签:
1条回答
  • 2021-01-16 16:04

    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.)

    0 讨论(0)
提交回复
热议问题