在Android程序中往往需要對(duì)圖片進(jìn)行處理,也就是將圖片解析為字節(jié)數(shù)組,讀取字節(jié)數(shù)組轉(zhuǎn)換成圖片,圖片Bitmap 和Drawable 的轉(zhuǎn)換,下邊寫(xiě)了3個(gè)方法去實(shí)現(xiàn)這寫(xiě)轉(zhuǎn)換
//bitmap 和 drawable的轉(zhuǎn)換
public static Bitmap drawableToBitmap(Drawable drawable){
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return bitmap;
}
//bitmap和byte[]的轉(zhuǎn)換
public byte[] getBitmapByte(Bitmap bitmap){
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
public Bitmap getBitmapFromByte(byte[] temp){
if(temp != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
return bitmap;
}else{
return null;
}
}