【发布时间】: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
-
更多答案 = 更多反对票。