0

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

        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

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

4

4 回答 4

6
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();
}
于 2012-05-18T02:34:11.803 回答
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 计数器重新开始。

所以我建议你用随机数连接文件名。

于 2012-05-18T02:43:29.823 回答
1

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();
        }
    }
于 2019-08-06T10:11:29.640 回答
0

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

于 2014-03-14T10:15:20.237 回答