单击按钮时,我成功地将图像保存在我的 sd 卡上。当我保存另一张图片时,我的应用程序崩溃了。我没有覆盖以前的图像,因为我希望图像名称保持不变。我怎样才能做到这一点。
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();
}