user2055370
user2055370

Reputation:

Single filename for captured images, overwriting the previous image, i.e.."TestPic.jpg"

Each time I take a photo, I want the image name to be "TestPic.jpg" every time, but every time I click to take a pic, it saves as: TestPic2203 , TestPic4023, etc.

I wrote code to delete the previous image and keep only one image in that folder which is replaced by next picture.

public static File CreateImageFile() throws IOException {


        String ImageFileName = "TestPic";
        File storageDir = new File(Environment.getExternalStorageDirectory()+"/EasyRecharge");
        if(!storageDir.exists()){
            File EasyRechargeDir = new File(Environment.getExternalStorageDirectory().getPath()+"/EasyRecharge/");
            EasyRechargeDir.mkdirs();
        }
    File test = new File(Environment.getExternalStorageDirectory().getPath()+"/EasyRecharge/TestPic.jpg");
    if(test.exists()){
        test.delete();
    }

        File image = File.createTempFile(ImageFileName,// prefix
                ".jpg", // suffix
                storageDir
                );// directory
        AppLog.showAppFlow("file created");
        AppLog.showAppFlow("image filename:"+image);

        return image;

    }

Upvotes: 1

Views: 51

Answers (1)

Harald K
Harald K

Reputation: 27084

If you want the file name to be the same each time, don't use File.createTempFile(). It's purpose is exactly to create a unique name for each file...

Instead use:

 File image = new File(storageDir, ImageFileName + ".jpg");

This way, the filename will be the same each time.

Upvotes: 1

Related Questions