简体   繁体   English

保存到sdcard android时图像被覆盖

[英]Image gets overwritten when saving to sdcard android

I want to save multiple images in a folder on sdcard but when saving new image, previous ones gets overwritten. 我想将多个图像保存在sdcard的文件夹中,但是保存新图像时,以前的图像会被覆盖。 How can I save multiple images in my folder? 如何在文件夹中保存多张图像?

    File mydirectory =  new File(Environment.getExternalStorageDirectory() + "/AppFolder");
    if(!mydirectory.exists()){

        mydirectory.mkdir();
    }

    if(mydirectory.exists()){
        try {
            File root = new File(Environment.getExternalStorageDirectory() +"/AppFolder");


            File sdImageMainDirectory = new File(root , "pic.png");
            outputFileUri = Uri.fromFile(sdImageMainDirectory);
            startCameraActivity();

        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();

            finish();
        }
    }
}

private void startCameraActivity() {
    // TODO Auto-generated method stub

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, 101);
}

You are saving each file with name as pic.png . 您将每个文件的名称保存为pic.png

You need to change the file name each time. 您需要每次更改文件名。

Edit: 编辑:

In addition to answer by @David Xu, if you want to have unique name each time, you can just append a unix timestamp at the end of the file name as follows 除了@David Xu的回答外,如果您希望每次都具有唯一的名称,则可以在文件名的末尾附加一个unix时间戳,如下所示

dImageMainDirectory = new File(root, "pic_" + (System.currentTimeMillis() / 1000L) + ".png");

change 更改

File sdImageMainDirectory = new File(root , "pic.png");

to

File sdImageMainDirectory = null;
int i = 0;
do {
   sdImageMainDirectory = new File(root, "pic-" + i + ".png");
   i++;
} while (sdImageMainDirectory.exists());

You are using the same name for every picture which is why it is getting overwritten 您为每张图片使用相同的名称,这就是为什么它被覆盖的原因

File sdImageMainDirectory = new File(root , "pic.png");

you are naming them all pic.png 您将它们全部命名为pic.png

Try developing a naming convention like pic1.png, pic2.png or you can save the png as the name of the current timestamp like this: 尝试开发诸如pic1.png,pic2.png之类的命名约定,或者可以将png保存为当前时间戳的名称,如下所示:

//get timestamp into string
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date today = Calendar.getInstance().getTime();        
String timeStamp = df.format(today);

File sdImageMainDirectory = new File(root , timeStamp + ".png");

Every file you create has the same name. 您创建的每个文件都具有相同的名称。 Here's a quick and dirty solution: 这是一个快速而肮脏的解决方案:

            try{
                File root = new File(Environment.getExternalStorageDirectory() +"/AppFolder");

                int i = 0;
                File sdImageMainDirectory = new File(root , "pic.png");
                while(sdImageMainDirectory.exists()){
                     i++
                     sdImageMainDirectory = new File(root , "pic" +i+ ".png");
                }
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                startCameraActivity();

             } catch (Exception e) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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