Android画布保存时总是出现java.io.IOException: open failed: ENOENT(没有这样的文件或目录)错误。

10

我有一个画布应用程序。我正在尝试使用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();
    }
}

我不知道为什么,但它总是出错或进入catch语句,并显示此错误:

java.io.IOException: open failed: ENOENT (No such file or directory)
4个回答

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

更新

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

File mFolder = new File(extr + "/imotax");

在设备上运行还是模拟器? - Biraj Zalavadia
设备出现错误:09-04 12:28:07.863:I/ttd(16524):java.io.FileNotFoundException:/storage/sdcard0/imotax/capture/spop/ttd/image/tmp.png:打开失败:ENOENT(没有这样的文件或目录) - yozawiratama
嘿,它可以保存图像,真的谢谢 :),但为什么只有黑色?:( - yozawiratama
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 并尝试移除。 - Biraj Zalavadia
我仍然面临问题。/storage/emulated/0/Download/tmp.png:打开失败:EACCES(权限被拒绝) - AEMLoviji
@AEMLoviji,你在Android清单文件中添加了外部存储写入权限吗? - Biraj Zalavadia

9
Add this permissions in manifest.

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

7
根据 Google 的指南,写权限也意味着读取权限,因此不需要声明两者。详见 http://developer.android.com/training/camera/photobasics.html。 - Mark Bridges

0

不要问我为什么,但这似乎是一个访问权限的问题。尝试使用一些公共目录。可以使用类似以下的内容:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

0
问题在于您在sdcard0之间漏掉了一个斜杠“/”。
storage/sdcard0/imotax/capture/spop/ttd/image/tmp.png
should be
storage/sdcard/0/imotax/capture/spop/ttd/image/tmp.png

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接