如何在不覆盖最后拍摄的图像的情况下将图像保存在外部文件夹中

时间:2014-11-23 09:41:52

标签: android-intent android-camera

我有一个应用程序,我想拍照并保存到手机/设备上的文件夹中。我遇到的问题是,当我拍摄照片时,它将其保存到该目录中,但是当拍摄另一张照片时,它会替换该文件夹中的上一张照片。我的问题是如何在不替换上一张照片的情况下保存下一张照片?

这是我的camere意图代码:

@Override
public void onMapLongClick(Intent intent) {
Intent getCameraImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File cameraFolder;
if  (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new  File(android.os.Environment.getExternalStorageDirectory(),"HikingImages/");
else
    cameraFolder= context.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();
String imageFileName = imagename;
photo = new File(cameraFolder, "HikingImages/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri.fromFile(photo);
startActivityForResult(getCameraImage, TAKE_PICTURE);

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

尝试在文件名

中添加时间戳或递​​增编号

http://developer.android.com/training/camera/photobasics.html#TaskPath

而不是:

 String imageFileName = imagename;
 photo = new File(cameraFolder, "HikingImages/" + imageFileName);

这样做:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = imagename + "_" + timeStamp;
photo = new File(cameraFolder, "HikingImages/" + imageFileName);