Skip to content
Advertisement

save image file with increment at the end of file name

enter image description here

please someone help me “To save an image file with increment at end of file name” like(“image 1.jpg , image 2.jpg , etc..”)

here is my code

please some help me to make this,i am new learner to android-studio.

private File saveBitMap(Context context, View drawView) {

    File pictureFileDir = new File(Environment.getExternalStorageDirectory()+"/"+"Frames");

    if (!pictureFileDir.exists()) {
        boolean isDirectoryCreated = pictureFileDir.mkdirs();
        if(!isDirectoryCreated) {
            Log.i("ATG", "Can't create directory to save the image");
        }
        return null;
    }
    String filename = pictureFileDir.getPath() +File.separator+"Frame"+ System.currentTimeMillis()+".jpg";
    File pictureFile = new File(filename);
    Bitmap bitmap =getBitmapFromView(drawView);
    try {
        pictureFile.createNewFile();
        FileOutputStream oStream = new FileOutputStream(pictureFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
        oStream.flush();
        oStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue saving the image.");
    }
    scanGallery( context,pictureFile.getAbsolutePath());
    return pictureFile;
}

Advertisement

Answer

If I have understood your question properly then, I assume you want your pictureFile name to be appended by an integer (in auto-incrementing fashion).

You could do that by maintaining a global variable as int imageCount = 1

and then appending it while creating fileName

int imageCount = 1;

private File saveBitMap(Context context, View drawView) {

    File pictureFileDir = new File(Environment.getExternalStorageDirectory()+"/"+"Frames");

    if (!pictureFileDir.exists()) {
        boolean isDirectoryCreated = pictureFileDir.mkdirs();
        if(!isDirectoryCreated) {
            Log.i("ATG", "Can't create directory to save the image");
        }
        return null;
    }
    String filename = pictureFileDir.getPath() +File.separator+"Frame"+ System.currentTimeMillis()+""+(imageCount++)+".jpg";
    File pictureFile = new File(filename);
    Bitmap bitmap =getBitmapFromView(drawView);
    try {
        pictureFile.createNewFile();
        FileOutputStream oStream = new FileOutputStream(pictureFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
        oStream.flush();
        oStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue saving the image.");
    }
    scanGallery( context,pictureFile.getAbsolutePath());
    return pictureFile;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement