1、Drawable --> Bitmap

 Bitmap drawable2Bitmap(Drawable drawable) {  if (drawable instanceof BitmapDrawable) {   return ((BitmapDrawable) drawable).getBitmap();  } else if (drawable instanceof NinePatchDrawable) {   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);   drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),     drawable.getIntrinsicHeight());   drawable.draw(canvas);   return bitmap;  } else {   return null;  } }


2、从资源中获取的Drawable --> Bitmap

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


3、Bitmap --> Drawable

 Drawable bitmap2Drawable(Bitmap bitmap) {  return new BitmapDrawable(bitmap); }

4、Bitmap --> byte[]

 byte[] Bitmap2Bytes(Bitmap bm) {  ByteArrayOutputStream baos = new ByteArrayOutputStream();  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  return baos.toByteArray(); }


5、 byte[] --> Bitmap

 Bitmap Bytes2Bimap(byte[] b) {  if (b.length != 0) {   return BitmapFactory.decodeByteArray(b, 0, b.length);  } else {   return null;  } }


更多相关文章

  1. android 获取未安装apk的icon、name等信息
  2. android中获取wifi信息
  3. Android点击通知栏消息,仅打开App,不跳转到具体Activity
  4. Android获取有线和无线(wifi)的IP地址
  5. android用于打开各种文件的intent
  6. android获取图库图片并返回
  7. android的PowerManager和PowerManager.WakeLock and WiFi Lock
  8. Android获取传感器列表
  9. Android(安卓)获取JSP或ASP的sessionId(Cookie)

随机推荐

  1. android 中apk如何防止反编译?
  2. Android(安卓)API——自定义组件(Custom C
  3. android API key 如何获取
  4. 详解Android获取系统内核版本的方法与实
  5. Android源码在线阅读
  6. Android应用程序的调试方法
  7. Android第一行代码(第二版)
  8. Android(安卓)Bundle类
  9. android tips:从资源文件中读取文件流显
  10. Android调节屏幕亮度工具类BrightnessUti