安卓图片转换

安卓图片处理转换

/**
* 对安卓图片的处理
*/
public class GetPicture {
   //drawable转化为二进制图片数据
   public static byte[] getPicture(Drawable drawable) {
       if(drawable == null) {
           return null;
      }
       BitmapDrawable bd = (BitmapDrawable) drawable;
       Bitmap bitmap = bd.getBitmap();
       ByteArrayOutputStream os = new ByteArrayOutputStream();
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
       return os.toByteArray();
  }
//二进制图片数据转化为drawable
   public static Drawable getPicture(byte[] b, Context context) {
       if(b!=null){
           Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
           Drawable bitmapDrawable = new BitmapDrawable(context.getResources(),bitmap);
           return bitmapDrawable;
      }
       return null;
  }
}
posted on 2021-11-29 22:19  jeremie_my00  阅读(43)  评论(0)    收藏  举报