我使用下面的代码将图像保存在SD卡中,但我继续将此问题置于异常之下
- private void SaveImage(Bitmap finalBitmap,String filename) {
- String root = Environment.getExternalStorageDirectory().toString();
- File myDir = new File(root + "/saved_images");
- myDir.mkdirs();
- String fname = filename;
- File file = new File (myDir,fname);
- if (file.exists ()) file.delete ();
- try {
- FileOutputStream out = new FileOutputStream(file);
- finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
- out.flush();
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
我错过了什么吗?
解决方法
在您不创建目录时修改代码.
- private void SaveImage(Bitmap finalBitmap,String filename) {
- String root = Environment.getExternalStorageDirectory().toString();
- File myDir = new File(root + "/saved_images");
- myDir.mkdirs();
- String fname = filename;
- File file = new File (myDir,fname);
- if (file.exists ()) file.delete ();
- file.createNewFile();
- try {
- FileOutputStream out = new FileOutputStream(file);
- finalBitmap.compress(Bitmap.CompressFormat.JPEG,out);
- out.flush();
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
}