图像不会覆盖相同的名称

问题描述 投票:1回答:1

我正在开发图像编辑器应用..因此每次用户都必须保存图像。所以首先我插入

  String savedImageURL = MediaStore.Images.Media.insertImage(
                        getContentResolver(),
                        bitmap,
                        "Bird",
                        "Image of bird"
                );

这段代码,但它创建新文件而不是覆盖。

所以我用另一种方法

public String saveImage(String folderName, String imageName) {
 String selectedOutputPath = "";
      if (isSDCARDMounted()) {
          File mediaStorageDir = new File(
                  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName);
          // Create a storage directory if it does not exist
          if (!mediaStorageDir.exists()) {
              if (!mediaStorageDir.mkdirs()) {
                  Log.d("PhotoEditorSDK", "Failed to create directory");
              }
          }
          // Create a media file name
          selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
          Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath);
     File file = new File(selectedOutputPath);
     try {
                    FileOutputStream out = new FileOutputStream(file,true);
                    if (parentView != null) {
                        parentView.setDrawingCacheEnabled(true);
                        parentView.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 80, out);
                    }
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
     }
     return selectedOutputPath;
       }

但它也没有用。

有谁知道覆盖同名的位图?

java android bitmap overwrite
1个回答
1
投票

将false作为第二个参数传递,将append设置为false,以便覆盖现有文件:

 FileOutputStream out = new FileOutputStream(file,false);

看看构造函数documentation

这是你的代码:

public String saveImage(String folderName, String imageName) {
 String selectedOutputPath = "";
    if (isSDCARDMounted()) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName);
      // Create a storage directory if it does not exist
      if (!mediaStorageDir.exists()) {
          if (!mediaStorageDir.mkdirs()) {
              Log.d("PhotoEditorSDK", "Failed to create directory");
          }
      }
      // Create a media file name
      selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
      Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath);
 File file = new File(selectedOutputPath);

if (file.exists()) 
{
  try {
            file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


 try {
 file.createNewFile();
                FileOutputStream out = new FileOutputStream(file,false);
                if (parentView != null) {
                    parentView.setDrawingCacheEnabled(true);
                    parentView.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 80, out);
                }
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
 }
 return selectedOutputPath;
   }
© www.soinside.com 2019 - 2024. All rights reserved.