在SD卡android中保存图像覆盖

在SD卡android中保存图像覆盖,android,image,sd-card,save,overwrite,Android,Image,Sd Card,Save,Overwrite,我使用以下代码来保存图像 FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame); // File root = Environment.getExternalStorageDirectory(); // File file = new File(root, "androidlife.jpg"); // File file = ne

我使用以下代码来保存图像

        FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame);
            //  File root = Environment.getExternalStorageDirectory();
            //  File file = new File(root, "androidlife.jpg");
//              File file = new File(Environment.getExternalStorageDirectory()
//                        + File.separator + "/test.jpg");
                Random fCount = new Random();
                // for (int i = 0; i < 10; i++) { Comment by Lucifer
                  int roll = fCount.nextInt(600) + 1;
                  //System.out.println(roll);


                File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "/test" + String.valueOf(roll) +".jpg" );

                Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(),
                        mainLayout.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(b);
                mainLayout.draw(c);
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);

                    if (fos != null) {
                        b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                        fos.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            //  }  Comment by Lucifer
FrameLayout mainloayout=(FrameLayout)findViewById(R.id.frame);
//文件root=Environment.getExternalStorageDirectory();
//File File=新文件(根,“androidlife.jpg”);
//File File=新文件(Environment.getExternalStorageDirectory()
//+File.separator+“/test.jpg”);
Random fCount=新的Random();
//对于(inti=0;i<10;i++){路西法的评论
整卷=fCount.nextInt(600)+1;
//系统输出打印项次(卷);
File File=新文件(Environment.getExternalStorageDirectory()
+File.separator+“/test”+String.valueOf(roll)+“.jpg”);
位图b=Bitmap.createBitmap(mainLayout.getWidth(),
mainLayout.getHeight(),Bitmap.Config.ARGB_8888);
画布c=新画布(b);
主布局图(c);
FileOutputStream=null;
试一试{
fos=新文件输出流(文件);
如果(fos!=null){
b、 压缩(位图.CompressFormat.JPEG,90,fos);
fos.close();
}
}捕获(例外e){
e、 printStackTrace();
}
//}路西法评论

它完美地保存了图像,但当我按两次“保存”按钮时会覆盖…会出现什么问题?任何建议???

您已经给出了一个静态文件名

File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "/test.jpg");
if(!file.exists()){
    try {
      file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}else{
    file.delete();
    try {
        file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(),
mainLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
mainLayout.draw(c);
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(file);
    if (fos != null) {
        b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.close();
    }
} catch (Exception e) {
    e.printStackTrace();
}
File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "/test.jpg");
所以,每次它都要在同一位置创建一个名为test.jpg的图像。您需要实现的唯一逻辑是将文件名更改为动态文件名。你可以这样试试

静态整数fCount=0

File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "/test" + String.valueOf(fCount++) +".jpg" );
现在,上面的代码行每次都将创建一个新文件,从名称test0.jpg、test1.jpg开始。。。等等

但当您关闭应用程序并重新启动应用程序时,这可能会产生问题。因为它将从0计数器重新开始


所以我建议你用一个随机数来表示文件名

只需在文件名中添加
System.currentTimeMillis()
,即可获得完整的唯一文件名。这将在文件名中添加自epoch以来的当前时间(毫秒),除非您可以在一毫秒内创建多个文件,否则不会进行覆盖。

sticker\u view.setLocked(true)

File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "/test.jpg");

请参阅上面的内容,而不是过度加载。如果文件存在,您可以删除该文件并重新创建新文件。另外,如果不想创建新文件(如果存在),只需在if块中编写代码并删除else块即可。我希望每次单击一次即可创建新文件,它既不应该重写也不应该删除以前对我有用的一项任务。我已经删除了旧文件,并创建了新文件。再次感谢。当你第二次按下按钮时,你到底希望/期望发生什么?@Lucifer我已经尝试了你的ans,它一次单击就保存了图像5次,保存在根文件夹中你能上传你的更新代码吗?@Lucifer找到我的更新代码!!!!为什么要使用for循环,只需删除它,并将代码准备就绪,即可进行堆栈。溢流解释一下为什么这会解决最初的问题会很有帮助。OP犯了什么错误?