android – java.io.FileNotFoundException:/storage/emulated/0/saved_images/grub.jpg:open failed:ENOENT(没有这样的文件或目录)

前端之家收集整理的这篇文章主要介绍了android – java.io.FileNotFoundException:/storage/emulated/0/saved_images/grub.jpg:open failed:ENOENT(没有这样的文件或目录)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用下面的代码将图像保存在SD卡中,但我继续将此问题置于异常之下
  1. private void SaveImage(Bitmap finalBitmap,String filename) {
  2.  
  3. String root = Environment.getExternalStorageDirectory().toString();
  4. File myDir = new File(root + "/saved_images");
  5. myDir.mkdirs();
  6.  
  7. String fname = filename;
  8. File file = new File (myDir,fname);
  9. if (file.exists ()) file.delete ();
  10. try {
  11. FileOutputStream out = new FileOutputStream(file);
  12. finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
  13. out.flush();
  14. out.close();
  15.  
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }

我错过了什么吗?

解决方法

在您不创建目录时修改代码.
  1. private void SaveImage(Bitmap finalBitmap,String filename) {
  2.  
  3. String root = Environment.getExternalStorageDirectory().toString();
  4. File myDir = new File(root + "/saved_images");
  5. myDir.mkdirs();
  6.  
  7. String fname = filename;
  8. File file = new File (myDir,fname);
  9. if (file.exists ()) file.delete ();
  10. file.createNewFile();
  11. try {
  12. FileOutputStream out = new FileOutputStream(file);
  13. finalBitmap.compress(Bitmap.CompressFormat.JPEG,out);
  14. out.flush();
  15. out.close();
  16.  
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }

}

猜你在找的Android相关文章