保存图片文件,文件名末尾附加递增数字。

huangapple go评论68阅读模式
英文:

save image file with increment at the end of file name

问题

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;
}
英文:

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;
}

答案1

得分: 0

如果我正确理解了您的问题,那么我假设您希望将 pictureFile 名称附加一个整数(自动递增方式)。

您可以通过维护一个全局变量 int imageCount = 1 来实现这一点。

然后在创建 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;
}
英文:

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;
}

huangapple
  • 本文由 发表于 2020年8月25日 01:47:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63566171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定