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

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

问题描述

我无法弄清楚这里出了什么问题......我已经尝试过更简洁地写这个,但是没有用。在阅读了有关此问题的其他建议后,我输入了所有额外的字符串。没有帮助。不知道发生了什么。它可能与权限相关吗? AFAIK我正在尝试写入内部内存并且不需要特殊权限?

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?

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

每次都是制作dirs,目录不是留下来的,或者是什么。当它到达myFile.createNewFile();它给出错误消息打开失败:ENOENT(没有这样的文件或目录)

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)"

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

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?

谢谢

推荐答案

你不是正确使用Environment.DIRECTORY_PICTURES。它本身不是文件夹,您需要将其用作getExternalStoragePublicDirectory()方法的参数。
点击此处: http://developer.android。 com / reference / android / os / Environment.html#getExternalStoragePublicDirectory(java.lang.String)

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)

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

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