【问题标题】:how can i overwrite a file if it exists in directory如果目录中存在文件,我该如何覆盖它
【发布时间】:2016-08-29 05:03:54
【问题描述】:

我将图像保存在同名的目录中(出于某种目的)但是当文件已经存在时它不会被覆盖,我怎么知道?因为当我保存这个文件时,已经存在的文件没有改变,但是当我为我的文件使用不同的名称时它可以工作。所以我想要的是用新文件替换现有文件。到目前为止,这就是我正在尝试的:

 content.setDrawingCacheEnabled(true);
                Bitmap bitmap = content.getDrawingCache(); // an bitmap file for saving


                File file = new File(context.getApplicationContext().getFilesDir() + "/img.jpg"); //here's the path where i'm saving my file

                if (file.exists()){

                    file.delete(); // here i'm checking if file exists and if yes then i'm deleting it but its not working 
                }


                String path = file.getPath();

                try {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 60, ostream);


                    savedFile = new File(path);


                    ostream.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();

                }

我正在删除现有文件后立即进行另一项检查:

                if (file.exists()){

                    Toast.makeText(context , "Still EXISTS",Toast.LENGTH_SHORT).show();
                }

这个时间文件不在这里,因为吐司没有出现。

【问题讨论】:

  • 我很想知道投反对票的原因。
  • 您使用的是 Android 4.4 或更高版本吗?
  • 它 5.1.0 @ScaryWombat
  • 更多答案 = 更多反对票。

标签: java android


【解决方案1】:

首先,让我们在你的 AndroidManifest.xml 文件中插入权限(低于 Android 6.0 版本)

android:name="android.permission.WRITE_INTERNAL_STORAGE" />

然后,使用下面的代码将位图写入文件

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "FitnessGirl"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = content.getDrawingCache();// Your bitmap
//Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close(); // do not forget to close the stream

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

【讨论】:

  • 只是部分文件。你可以删除它。我更新了它。有用就采纳吧
  • 嘿,抱歉回复晚了,正在工作,所以我尝试了您的代码,但效果仍然相同。查看我的代码
  • String path = CropActivity.this.getApplicationContext().getFilesDir().toString(); OutputStream fOut = null; File file = new File(path, "img"+".jpg"); savedFile = file; try { fOut = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); }
  • Bitmap pictureBitmap = content.getDrawingCache(); pictureBitmap = getImageBitmap(myurl); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); fOut.flush(); try { fOut.close(); } catch (IOException e) { e.printStackTrace(); }
  • try { MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); } Log.d("saved",""+savedFile.toString());
【解决方案2】:

将此行添加到您的代码中

FileWriter fw = new FileWriter(myDir + "/score.jpg", false);

【讨论】:

  • 这是为了保存字符
【解决方案3】:

试试这个,

boolean result = Files.deleteIfExists(file.toPath()); 

还要确保您已启用写入权限!

PS:在 Android Lollipop 中,您需要 runtime permission,而清单文件中已设置的权限将不起作用。

【讨论】:

  • 糟糕,现在您将开始获得反对票并关闭请求;)
  • @Prokash runtime permission 这可能是原因,任何指导我如何提示该权限请求??
  • @remy 男孩请检查我的答案中的链接。我遇到了完全相同的问题。
猜你喜欢
  • 2017-10-29
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
相关资源
最近更新 更多