Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 954
yozawiratama Avatar asked Sep 04 '13 04:09

yozawiratama


2 Answers

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");
like image 85
Biraj Zalavadia Avatar answered Oct 21 '22 11:10

Biraj Zalavadia


Add this permissions in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 42
anjaneya Avatar answered Oct 21 '22 09:10

anjaneya