我正在实现一个保存功能,将自定义可绘制视图保存为图像。问题是JPG图像已创建,但它不包含任何内容,最后程序“不幸地停止工作”任何人都可以帮助我,请在这里是我的代码;
Bitmap b = drawView.getDrawingCache();
File storage =Environment.getExternalStorageDirectory();
File file = new File(storage,spinner.getSelectedItem().toString()+".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 95,fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
drawView.destroyDrawingCache();
答案 0 :(得分:0)
使用这个
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
并在清单
中添加此内容 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />