這個問題很簡單,我們可以:保存位爲JPEG文件
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();
}
}
但這種方法並非所有的成功:因爲「注:並不是所有的格式直接支持所有的位圖的configs,所以這是可能的從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.so我認爲保存方法不好,你能告訴我另一種保存方法嗎?的視圖或佈局
謝謝你的回答,但沒有效果 – pengwang 2012-01-10 09:27:01
我已經嘗試過,併爲我工作。如果我想拍攝特定視圖的屏幕截圖並將其保存爲位圖,則可以使用此視圖。 – 2012-01-10 09:31:49
是的,如果使用常見的環境它正確,但使用趕上相機預覽它失敗,保存文件JPEG是黑色的,但ImageView顯示正確 – pengwang 2012-01-10 10:12:43