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

倾然丶 夕夏残阳落幕 提交于 2019-12-04 03:00:47

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");
Add this permissions in manifest.

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

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
bluscreen

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!