java - 图像不覆盖同名

标签 java android bitmap overwrite

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

  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;
       }

但它也没有用。

有人知道用同名覆盖位图吗?

最佳答案

将 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;
   }

关于java - 图像不覆盖同名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47990758/

相关文章:

java - void 变量有什么用?

java - 克隆内部类定义的对象

android - 签署Android应用程序时如何解决无效的SHA1签名?

android - Android Keystore KMS对称加解密如何管理?

java - 有效加载位图 - 仍然出现内存不足错误

c++ - 遇到 C++ 位图问题

java - Jetty Websocket,关闭 session OpCode 12

java - struts2静态方法调用问题

java - 获取不同运营商的Cell ID

c# - 从 MemoryStream c# 解压缩 JPEG