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

错误:打开失败: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.