错误:打开失败:ENOENT(没有这样的文件或目录) [英] Error: open failed: ENOENT (No such file or directory)

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

问题描述

我试图创建一个文件来保存相机中的图片,结果我无法创建该文件. 但是我真的找不到错误.您可以看看并给我一些建议吗?

I was trying to create a file to save pictures from the camera, it turns out that I can't create the file. But I really can't find the mistake. Can you have a look at it and give me some advice?

    private File createImageFile(){
            File imageFile=null;
            String stamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String imageFileName="JPEG_"+stamp+"_";
            try {
                imageFile=File.createTempFile(imageFileName,".jpg",dir);
            } catch (IOException e) {
                Log.d("YJW",e.getMessage());
            }
            return  imageFile;
        }

我已经添加了权限.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

该方法总是会出现这样的错误:

The method always gives such mistakes:

打开失败:ENOENT(没有这样的文件或目录)

open failed: ENOENT (No such file or directory)

推荐答案

图片目录可能还不存在.不能保证在那里.

The Pictures directory might not exist yet. It's not guaranteed to be there.

,该代码使用mkdirs确保该目录存在:

In the API documentation for getExternalStoragePublicDirectory(), the code ensures the directory exists using mkdirs:

File path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");

try {
    // Make sure the Pictures directory exists.
    path.mkdirs(); 

...因此,就像在您createTempFile之前将path.mkdirs()添加到现有代码中一样简单.

...so it may be as simple as adding that path.mkdirs() to your existing code before you createTempFile.

这篇关于错误:打开失败:ENOENT(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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