Android:DCIM文件夹中的照片存储重复 [英] Android: Duplicate photo storage in DCIM folder

查看:509
本文介绍了Android:DCIM文件夹中的照片存储重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本机Android相机并将文件保存到应用程序数据文件夹(/mnt/sdcard/Pictures/).同时-在某些设备上-照片的另一个副本将保存到DCIM文件夹中.

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.

这是我的代码:

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);
}

如何防止DCIM文件夹中保存图像的其他副本?

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

我的问题是代码会产生
1张照片:三星Galaxy SIII,华为HUAWEI P2-6011等.
2张照片:Samsung Galaxy SI,Htc HTC One XL等.

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.

其他线程介绍了删除最后添加的图像到DCIM.这里的问题是没有问题的设备,例如DCIM上的Galaxy SIII和Imagename以及应用程序数据文件夹上的Imagename是不同的.

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.

非常感谢

推荐答案

AFAIK,您无法可靠地(独立于设备)告诉相机应用a)图像的保存位置,b)也只能保存一次.我不得不求助于这种方法:

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)只需通过删除putExtra(...)语句,让相机应用将图片保存到喜欢的地方即可:

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`

此设置(即 not 设置EXTRA_OUTPUT)将确保仅将一张图像保存到默认图片位置.

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

2)查找最后拍摄的照片,并保存其ID,以备日后安全检查. (查询最后一个图像ID,按DATE_TAKEN排序.)

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)触发捕获意图,然后在您的onActivityResult中再次获取最后拍摄的图像,并保存其ID,URI和路径.

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

4)如果您的新图片ID大于以前保存的图片ID,请继续操作,否则恐慌...

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

5)将原始图像文件(使用其路径)移动到首选位置.现在,原始文件已删除.

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

6)使用其URI删除原始媒体映像条目.现在,图像也从图库中删除了.

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

7)如果您还想删除缩略图,则需要类似地进行查询和删除,但我建议您不要这样做:设备重新启动或其他媒体扫描应刷新缩略图缓存.另外,删除原始图像后,您实际上很可能需要一小段时间的缩略图. (如果需要的时间更长,则需要小心:如果将照片移至私人应用程序目录(getExternalFilesDir(Environment.DIRECTORY_PICTURES)),媒体管理器将(能够)为您生成缩略图,因此您可能需要管理自己的缩略图.)

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

这篇关于Android:DCIM文件夹中的照片存储重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆