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卡中,但我继续将此问题置于异常之下
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();
}

}

原文链接:https://www.f2er.com/android/316710.html

猜你在找的Android相关文章