【发布时间】:2012-01-10 08:25:46
【问题描述】:
这个问题很简单,我们可以:
public static void writePhotoJpg(Bitmap data, String pathName) {
File file = new File(pathName);
try {
file.createNewFile();
// BufferedOutputStream os = new BufferedOutputStream(
// new FileOutputStream(file));
FileOutputStream os = new FileOutputStream(file);
data.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
但此方法并非全部成功:因为“注意:并非所有格式都直接支持所有位图配置,因此从 BitmapFactory 返回的位图可能处于不同的位深度,和/或可能丢失了每个像素的 alpha (例如 JPEG 仅支持不透明像素)。”在谷歌文档中。没有运气我有这个问题:在我使用的相机预览中:
private Bitmap printScreen() {
View view = this.getWindow().getDecorView();
// if (view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
Calendar c = Calendar.getInstance();
String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
// }
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
return bmp ;
}
所以当我使用 setImageBitmap(bmp) 时,看起来很好,但是当我打开保存 jpeg 文件时它是黑色 jpeg。所以我认为保存方法不太好,你能告诉我另一种保存方法吗?
【问题讨论】: