在android上覆盖sd卡上的图像
单击按钮后,我成功地在sd卡上保存了一个图像。当我保存另一张图片时,我的应用程序崩溃了。我没有覆盖以前的图像,因为我希望图像名称保持不变。我怎样才能做到这一点在android上覆盖sd卡上的图像,android,Android,单击按钮后,我成功地在sd卡上保存了一个图像。当我保存另一张图片时,我的应用程序崩溃了。我没有覆盖以前的图像,因为我希望图像名称保持不变。我怎样才能做到这一点 ByteArrayOutputStream stream=new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 90, stream); byte[] image=stream.toByteArray(); System.o
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] image=stream.toByteArray();
System.out.println("byte array:"+image);
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "myfile");
imagesFolder.mkdirs();
String fileName = "myfile.jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
fileName = "myfile.jpg";
output = new File(imagesFolder, fileName);
}
while (output.exists()){
fileName = "myfile.jpg";
output = new File(imagesFolder, fileName);
}
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
//bitmap image here
imageFileOS.write(image);
imageFileOS.flush();
imageFileOS.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
什么是
while (output.exists()){
fileName = "myfile.jpg";
output = new File(imagesFolder, fileName);
}
这将循环您的代码进行未限定
改用
if (output.exists()){
output.delete(); //DELETE existing file
fileName = "myfile.jpg";
output = new File(imagesFolder, fileName);
}
没有什么。只需为所有图像设置相同的名称。。