java.io.FileNotFoundException:/storage/emulated/0/saved_images/grub.jpg:打开失败:ENOENT(没有这样的文件或目录) [英] java.io.FileNotFoundException: /storage/emulated/0/saved_images/grub.jpg: open failed: ENOENT (No such file or directory)

查看:151
本文介绍了java.io.FileNotFoundException:/storage/emulated/0/saved_images/grub.jpg:打开失败:ENOENT(没有这样的文件或目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码将图像保存到sd卡中,但我一直在遇到以下异常情况

Am using the below code to save an image in sd card but I keep on getting this below exception

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();
    }
}

我在这里错过了什么吗?

Am I missing out anything here?

推荐答案

修改代码,因为您没有创建目录:

Modify your code, as you are not creating the directory:

 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, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

这篇关于java.io.FileNotFoundException:/storage/emulated/0/saved_images/grub.jpg:打开失败:ENOENT(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆