Android画布保存总是java.io.IOException:打开失败:ENOENT(没有这样的文件或目录)

yoz*_*ama 10 android android-canvas

我有一个画布应用程序.我正在尝试用Canvas+ 创建一个签名应用程序onTouchListener.

这是我的保存方法,我尝试将签名保存到图像:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,但它总是错误或进入catch带有此错误的语句:

java.io.IOException: open failed: ENOENT (No such file or directory)
Run Code Online (Sandbox Code Playgroud)

Bir*_*dia 12

试试这种方式

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

UPDATE

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

File mFolder = new File(extr + "/imotax");
Run Code Online (Sandbox Code Playgroud)


anj*_*eya 9

Add this permissions in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

  • 根据谷歌指南,写入权限也意味着阅读.所以不应该同时声明两者.http://developer.android.com/training/camera/photobasics.html (7认同)