简体   繁体   中英

Android- My pictures are overwriting each other

I'm a beginner android developer.I have made an android application that takes a photo and saves it into my gallery. However once I take a second photo it overwrites the first. Here is my code.

static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
public void onClickbtnCamera(View v)
{
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0));
    Uri uriSavedImage=Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera","QR_"+timeStamp+ ".png"));
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(imageIntent, 1); 
}

Basically I need the QR to add a number each time a photo is taking eg first photo (QR_) second photo (QR_1) third photo (QR_2). Thanks

Try changing new Date(0) to new Date() .

You're always using 1 January 1970 to create your timestamp, which means they always come out the same. With this change, you'll always use the current timestamp. As long as you don't generate two pictures within the same second, they'll never clash.

(If you're worried about taking photos faster than one per second, you could always include milliseconds in your date format.)

您将需要确保每次都不是时间戳记,然后使用任意随机对数生成随机字符串并附加到时间戳记。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM