我有一个画布应用程序。我正在尝试使用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)
答案 0 :(得分: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");
答案 1 :(得分:9)
Add this permissions in manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
不要问我原因,但这似乎是访问权限的问题。尝试使用一些公共目录。使用类似的东西:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
答案 3 :(得分:-1)
问题是你错过了一个&#34; /&#34;在 sdcard 和 0
之间storage/sdcard0/imotax/capture/spop/ttd/image/tmp.png
should be
storage/sdcard/0/imotax/capture/spop/ttd/image/tmp.png