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. Android(安卓)获取手机的IMEI等设备信息
  2. android 获取不同res下的资源id
  3. Android中Bitmap、Drawable、byte[]转换
  4. android如何获取经纬度
  5. android 6.0及以上危险权限的获取
  6. Android获取物理地址(支持5.0~10.0)
  7. Android透明字体画法
  8. Android中的获取内存信息的相关命令
  9. Android获取StatusBa有效高度

随机推荐

  1. Android开发之Android体系架构介绍
  2. 小编程(三):用户登录注册界面开发及用户信息
  3. Android(安卓)进阶—— Android(安卓)系
  4. 与Android有关的三起诉讼事件
  5. Android(安卓)5.0 技术新趋势
  6. Android之地理信息服务·Android的定位服
  7. Android之网络丢包事件
  8. Android工程的编译过程
  9. 浅析Android线程模型
  10. Android(安卓)透明状态栏 沉浸式状态栏的