【问题标题】:Saving image overwrite in sdcard android在 sdcard android 中保存图像覆盖
【发布时间】:2012-05-25 14:50:27
【问题描述】:

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

        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

它完美地保存了图像,但是当我按两次保存按钮时会覆盖...可能是什么问题?有什么建议吗??

【问题讨论】:

  • 第二次按下按钮时,您希望/期望发生什么?
  • @Lucifer 我试过你的答案,它一次点击保存图像 5 次,并保存在根文件夹中
  • 你能上传你更新的代码吗?
  • @Lucifer 找到我更新的代码!!!
  • 为什么要使用 for 循环,只需删除它,您的代码就准备好了亲爱的

标签: android image sd-card save overwrite


【解决方案1】:
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();
}

【讨论】:

  • 请参阅上面的内容,而不是过度使用您可以删除文件(如果存在)并重新创建新文件。另外,如果您不想创建新文件(如果存在),则只需将代码写入 if 块并删除 else 块。
  • 我想每次点击创建一个新文件,它既不能覆盖也不能删除之前的文件
  • 谢谢,这对我有用..我删除了旧文件并创建了新文件,再次感谢..
【解决方案2】:

你给了一个静态文件名。

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

所以,每次它都会在同一位置创建一个名为 test.jpg 的图像。您需要实现的唯一逻辑是将文件名更改为动态文件名。你可以这样试试

静态 int fCount = 0;

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

现在上面的行每次都会创建一个新文件,以名称 test0.jpg、test1.jpg ... 开始,以此类推。

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

所以我建议您使用带有文件名的随机数连接。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      sticker_view.setLocked(true);

              sticker_view.setDrawingCacheEnabled(true);
              Bitmap bitmap = sticker_view.getDrawingCache();
              Log.e("BITMAP", "onOptionsItemSelected: " + bitmap);
      
              String root = Environment.getExternalStorageDirectory().toString();
              File newDir = new File(root + "/Edited Image");
              newDir.mkdirs();
      
              Random gen = new Random();
              int n = 10000;
              n = gen.nextInt(n);
              String photoName = "Image-" + n + ".jpg";
              Log.e("PHOTONAME", "onOptionsItemSelected: " + photoName);
      
              File file = new File(newDir, photoName);
              String filePath = file.getAbsolutePath();
              Log.e("FILEPATH", "onOptionsItemSelected: " + filePath);
      
              if (!file.exists()) {
                  try {
                      file.createNewFile();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  Toast.makeText(EditActivity.this, "Image Already Exist.", Toast.LENGTH_SHORT).show();
              } else {
                  file.delete();
                  try {
                      file.createNewFile();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              try {
                  FileOutputStream out = new FileOutputStream(file);
                  Log.e("OUT", "onOptionsItemSelected: " + out);
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                  out.flush();
                  out.close();
                  bitmap.recycle();
      
                  Toast.makeText(EditActivity.this, "Saved In Edited Image.", Toast.LENGTH_SHORT).show();
                  item.setVisible(false);
      
                  MediaScannerConnection.scanFile(EditActivity.this, new String[]{file.getAbsolutePath()},
                          null, new MediaScannerConnection.OnScanCompletedListener() {
                              public void onScanCompleted(String path, Uri uri) {
                                  Log.i("ExternalStorage", "Scanned " + path + ":");
                                  Log.i("ExternalStorage", "-> uri=" + uri);
                              }
                          });
      
                  Intent intent = new Intent();
                  intent.setAction("URI");
                  intent.putExtra("uri", filePath);
                  sendBroadcast(intent);
                  finish();
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      

      【讨论】:

      • 欢迎来到 Stack。溢出。解释为什么这会解决原始问题将非常有帮助。 OP 犯了什么错误?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      相关资源
      最近更新 更多