1.Drawable—>Bitmap

Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);
Resources res=getResources();
private byte[] Bitmap2Bytes(Bitmap bm){

2.Bitmap---->Drawable

Drawable drawable =new BitmapDrawable(bmp);


3、Drawable → Bitmap

public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap.createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

//canvas.setBitmap(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

4、从资源中获取Bitmap

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

5、Bitmap → byte[]

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray(); }

6、 byte[] → Bitmap

private Bitmap Bytes2Bimap(byte[] b){

if(b.length!=0){

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

else {

return null;

}

}


更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. Android屏幕分辨率正确获取及PX,DPI,DP,SP等的对应关系
  3. Android(安卓)Resource介绍和使用
  4. android 获取唯一标识
  5. android拍照与读取相册
  6. Android(安卓)热点开关状态的判断和获取热点ssid
  7. Android软键盘适配问题
  8. AIR Native Extension的使用(Android)一 : 打包ane
  9. Android(安卓)组件资源库

随机推荐

  1. Android(安卓)JNI介绍
  2. Android(安卓)解决ListView 和 ScrollVie
  3. 解决 Android(安卓)在Eclipse 开发中 Cla
  4. Android(安卓)布局 精准定位 平衡定位 相
  5. android之SharedPreferences进行数据存储
  6. android salite3 使用实例
  7. cocos2d-x学习笔记06:如何将win32移植到an
  8. Android应用开发中的风格和主题(style,th
  9. Android(安卓)Touch事件分发机制详解之由
  10. Android深入浅出之Audio 第一部分 AudioT