Android canvas save always java.io.IOException: open failed: ENOENT (No such file or directory)
Asked Answered
P

4

10

I have a canvas application. I'm trying to create a signature app with Canvas + onTouchListener.

This is my save method, where I try to save the signature to an image:

private void save() {
    hideMenuBar();
    View content = this;
    content.setDrawingCacheEnabled(true);
    content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    Bitmap bitmap = content.getDrawingCache();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    String imgPath = path+"/imotax/capture/spop/ttd/image" + "temp" + ".jpg";
    File file = new File(imgPath);
    FileOutputStream ostream;
    try {
        file.createNewFile();
        ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, ostream);
        ostream.flush();
        ostream.close();
        Toast.makeText(getContext(), "image saved", 5000).show();
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("ttd", e.toString());
        Toast.makeText(getContext(), "Failed To Save", 5000).show();
        showMenuBar();
    }
}

I don't know why, but it always errors or enters the catch statement with this error:

java.io.IOException: open failed: ENOENT (No such file or directory)
Politick answered 4/9, 2013 at 4:31 Comment(0)
W
12

Try this way

private void save() {
        try {
            hideMenuBar();
            View content = this;
            content.setDrawingCacheEnabled(true);
            content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap bitmap = content.getDrawingCache();

            String extr = Environment.getExternalStorageDirectory().toString();
            File mFolder = new File(extr + "/imotax/capture/spop/ttd/image");
            if (!mFolder.exists()) {
                mFolder.mkdir();
            }

            String s = "tmp.png";

            File f = new File(mFolder.getAbsolutePath(), s);

            FileOutputStream fos = null;
            fos = new FileOutputStream(f);
            bitmap.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();

            bitmap.recycle();

            Toast.makeText(getContext(), "image saved", 5000).show();
        } catch (Exception e) {
            Toast.makeText(getContext(), "Failed To Save", 5000).show();
        }
    }

UPDATE

File mFolder = new File(extr + "/imotax/capture/spop/ttd/image");  //replace with

File mFolder = new File(extr + "/imotax");
Wormy answered 4/9, 2013 at 4:41 Comment(6)
running on device or emulator?Wormy
device this is error : 09-04 12:28:07.863: I/ttd(16524): java.io.FileNotFoundException: /storage/sdcard0/imotax/capture/spop/ttd/image/tmp.png: open failed: ENOENT (No such file or directory)Politick
hey it can save the image, really thanks :) , but why only black? :(Politick
remove this line content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); and tryWormy
i am still facing problem. /storage/emulated/0/Download/tmp.png: open failed: EACCES (Permission denied)Reviewer
@Reviewer did you added permission for write external storage in Android manifest fileWormy
W
9
Add this permissions in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Windsucking answered 24/1, 2014 at 5:24 Comment(1)
According to Googles guides Write permission also implies Read. So there should be no need to declare both. developer.android.com/training/camera/photobasics.htmlAforetime
C
0

the promlem is that you miss a "/" between sdcard and 0

storage/sdcard0/imotax/capture/spop/ttd/image/tmp.png
should be
storage/sdcard/0/imotax/capture/spop/ttd/image/tmp.png
Cordy answered 25/1, 2015 at 10:2 Comment(0)
A
0

Don't ask me why but this seems to be a problem with access rights. Try to use some public directory. Using something like:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Assimilable answered 6/1, 2016 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.