在sdcard android中保存图像覆盖

时间:2012-05-18 01:40:46

标签: 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 = 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 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:2)

您已经提供了静态文件名。

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

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

static int fCount = 0;

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

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

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

所以我建议您使用文件名随机编号。

答案 2 :(得分: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();
        }
    }

答案 3 :(得分:0)

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

相关问题