gpt4 book ai didi

Android Canvas 总是保存java.io.IOException : open failed: ENOENT (No such file or directory)

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:15 35 4
gpt4 key购买 nike

我有一个 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();
}
}

不知道为什么,总是报错或者进入catch语句报错:

java.io.IOException: open failed: ENOENT (No such file or directory)

最佳答案

试试这个方法

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");

关于Android Canvas 总是保存java.io.IOException : open failed: ENOENT (No such file or directory),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605440/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com