Java createNewFile - 打开失败:ENOENT(没有那个文件或目录)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22435061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:42:29  来源:igfitidea点击:

createNewFile - open failed: ENOENT (No such file or directory)

javaandroid

提问by user3425269

I can't figure out what's going wrong here...I've tried writing this more succicinctly, that didn't work. I put in all the extra strings after reading other suggestions with this problem. Not helping. No clue what's happening. Could it be permissions-related? AFAIK I'm trying to write to internal memory and that doesn't need special permissions?

我不知道这里出了什么问题……我试过写得更简洁,但没用。在阅读了有关此问题的其他建议后,我输入了所有额外的字符串。没有帮助。不知道发生了什么。会不会是权限相关的?AFAIK 我正在尝试写入内部存储器并且不需要特殊权限?

public void outputBitmap(){ 
    String path = Environment.DIRECTORY_PICTURES.toString();
    File folder = new File(path + "/Blabla");
    String filename = new SimpleDateFormat("yyMMddHHmmss").format(Calendar.getInstance().getTime()) + ".png";
    try {
        if (!folder.exists()) {
            folder.mkdirs();
            System.out.println("Making dirs");
        }
        File myFile = new File(folder.getAbsolutePath(), filename);
        myFile.createNewFile();

        FileOutputStream out = new FileOutputStream(myFile);
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

It goes "Making dirs" every time, the directory is not staying made, or something. When it gets to myFile.createNewFile(); it gives the error message "open failed: ENOENT (No such file or directory)"

每次都会“制作目录”,目录没有保持制作状态,或者什么的。当它到达 myFile.createNewFile(); 它给出了错误消息“打开失败:ENOENT(没有这样的文件或目录)”

Not sure if it's related, but the information I am trying to output is from:

不确定它是否相关,但我试图输出的信息来自:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    myBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.RGB_565);
    Canvas pngCanvas = new Canvas(myBitmap);
    ...[some maths and stuff]
    canvas.drawLine(...);
    pngCanvas.drawLine(...);
}

I thought I should be able to use the same canvas for the bitmap, but that caused crashed, so I'm writing the same information to both canvases. So...I don't know if that's related to the issue or a totally different bad issue or what.

我认为我应该能够为位图使用相同的画布,但这导致崩溃,所以我将相同的信息写入两个画布。所以...我不知道这是否与问题有关,还是一个完全不同的坏问题或什么。

Been searching all kinds of questions that seemed similar, but couldn't find any solutions that worked for me. I've been trying to solve this for days now. Anyone know what's going wrong?

一直在搜索各种看起来相似的问题,但找不到任何对我有用的解决方案。这几天我一直在努力解决这个问题。有谁知道出了什么问题?

Thanks

谢谢

采纳答案by Hakan Serce

You are not using Environment.DIRECTORY_PICTURES correctly. It is not a folder by itself, you need to use it as a parameter to getExternalStoragePublicDirectory() method. Check here : http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

您没有正确使用 Environment.DIRECTORY_PICTURES。它本身不是一个文件夹,您需要将其用作 getExternalStoragePublicDirectory() 方法的参数。在这里检查:http: //developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

回答by Farhan

Possible Issue:

可能的问题:

Make sure you have given following required permission in your manifest file.

确保您已在清单文件中授予以下所需权限。

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

And for Marhsmallow devices, make sure Contacts Groups Permissions is granted too by device user.
Ref: http://developer.android.com/training/permissions/requesting.html

对于 Marhsmallow 设备,请确保设备用户也授予联系人组权限。
参考:http: //developer.android.com/training/permissions/requesting.html

回答by Marcelo Gumiero

Just change the begining of your code from this:

只需从此更改代码的开头:

public void outputBitmap(){ 
    String path = Environment.DIRECTORY_PICTURES.toString();
    File folder = new File(path + "/Blabla");

To this:

对此:

public void outputBitmap(){File folder = new File(getActivity().getExternalFilesDir(null) + IMAGE_DIRECTORY + "whatever you want for your directory name");