找不到文件例外:打开失败:ENOENT

时间:2016-08-10 08:18:09

标签: java android

我是android的新手,我从URL下载图片并在listView中设置。它工作一些移动设备而不是在某些移动设备上创建文件/目录。 它的抛出错误如:

java.io.FileNotFoundException: /storage/emulated/0/.tam/veg.png: open failed: ENOENT (No such file or directory)

我不知道为什么它的抛出错误就像这样一些移动设备。我想创建所有类型的移动目录。请有人帮助我。

这是我的代码:

public class ImageStorage {
public static String saveToSdCard(Bitmap bitmap, String filename) {

    String stored = null;

    File sdcard = Environment.getExternalStorageDirectory();
    File folder = new File(sdcard.getAbsoluteFile(), ".tam");//the dot makes this directory hidden to the user
    folder.mkdir();
    File file = new File(folder.getAbsoluteFile(), filename) ;
    if (file.exists())
        return stored ;
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
        stored = "success";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stored;
}

public static File getImage(String imagename) {

    File mediaImage = null;
    try {
        String root = Environment.getExternalStorageDirectory().getAbsolutePath();
        File myDir = new File(root);
        if (!myDir.exists())
            return null;

        mediaImage = new File(myDir.getPath() + "/.tam/"+imagename);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mediaImage;
}

public static File checkifImageExists(String imagename) {
    File file = ImageStorage.getImage("/" + imagename);
    if (file.exists()) {
        return file;
    } else {
        return null;
    }
}

public static String getImageName(String value){

    String getName[] = value.split("/");
    return getName[4];
}

}

以下路径并非所有移动设备:

/storage/emulated/0/

提前致谢!!

1 个答案:

答案 0 :(得分:0)

在您使用此路径之前,也许您应该检查移动设备中是否有外部存储空间

public String getDir(Context context) {  
String checkPath = null;  
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
        || !Environment.isExternalStorageRemovable()) {  
    checkPath = Environment.getExternalStorageDirectory().getPath();  
} else {  
    checkPath = context.getCacheDir().getPath();  
}  
return checkPath;  
}