读书人

图片转向二进制显示

发布时间: 2012-12-28 10:29:05 作者: rapoo

图片转为二进制显示
1.sdcard
ByteBuffer buff =ByteBuffer.allocate(1024);//分配缓冲区大小

try {
//Thread.sleep(15);
new FileInputStream("/sdcard/icon").getChannel().read(buff);
byte a[] = buff.array();//得到byte数组
ByteBuffer bb = ByteBuffer.wrap(a);
mBitmap = Bitmap.createBitmap(width,height , Bitmap.Config.RGB_565);
mBitmap.copyPixelsFromBuffer(bb);
canvas.drawBitmap(mBitmap,0, 0, null);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

2.raw
ByteBuffer buff =ByteBuffer.allocate(1024);//分配缓冲区大小
Resources res=getResources();
InputStream is=res.openRawResource(R.raw.icon);
BufferedInputStream bufferedInputStream=new BufferedInputStream(is);
int len;
try {
len = bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
ByteBuffer bb = ByteBuffer.wrap(bytes);
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
mBitmap.copyPixelsFromBuffer(bb);
canvas.drawBitmap(mBitmap, 0, 0, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

读书人网 >编程

热点推荐