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

Exception or error:

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

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?

Thanks

How to solve:

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)

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *