首页 > 代码库 > android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系
android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系
1 // 将Bitmap转换成InputStream(压缩率quality、100表示不压缩、10表示压缩90%) 2 public InputStream Bitmap2InputStream(Bitmap bm, int quality) { 3 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 4 bm.compress(Bitmap.CompressFormat.PNG, quality, baos); 5 InputStream is = new ByteArrayInputStream(baos.toByteArray()); 6 return is; 7 } 8 9 // 将Bitmap转换成InputStream 10 public InputStream Bitmap2InputStream(Bitmap bm) { 11 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 12 bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 13 InputStream is = new ByteArrayInputStream(baos.toByteArray()); 14 return is; 15 } 16 17 // 将InputStream转换成Bitmap 18 public Bitmap InputStream2Bitmap(InputStream is) { 19 return BitmapFactory.decodeStream(is); 20 } 21 22 // Drawable转换成InputStream 23 public InputStream Drawable2InputStream(Drawable d) { 24 Bitmap bitmap = this.drawable2Bitmap(d); 25 return this.Bitmap2InputStream(bitmap); 26 } 27 28 // InputStream转换成Drawable 29 public Drawable InputStream2Drawable(InputStream is) { 30 Bitmap bitmap = this.InputStream2Bitmap(is); 31 return this.bitmap2Drawable(bitmap); 32 } 33 34 // Drawable转换成byte[] 35 public byte[] Drawable2Bytes(Drawable d) { 36 Bitmap bitmap = this.drawable2Bitmap(d); 37 return this.Bitmap2Bytes(bitmap); 38 } 39 40 // byte[]转换成Drawable 41 public Drawable Bytes2Drawable(byte[] b) { 42 Bitmap bitmap = this.Bytes2Bitmap(b); 43 return this.bitmap2Drawable(bitmap); 44 } 45 46 // Bitmap转换成byte[] 47 public byte[] Bitmap2Bytes(Bitmap bm) { 48 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 49 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 50 return baos.toByteArray(); 51 } 52 53 // byte[]转换成Bitmap 54 public Bitmap Bytes2Bitmap(byte[] b) { 55 if (b.length != 0) { 56 return BitmapFactory.decodeByteArray(b, 0, b.length); 57 } 58 return null; 59 } 60 61 // 将byte[]转换成InputStream 62 public InputStream Byte2InputStream(byte[] b) { 63 ByteArrayInputStream bais = new ByteArrayInputStream(b); 64 return bais; 65 } 66 67 // 将InputStream转换成byte[] 68 public byte[] InputStream2Bytes(InputStream is) { 69 String str = ""; 70 byte[] readByte = new byte[1024]; 71 int readCount = - 1; 72 try { 73 while ((readCount = is.read(readByte, 0, 1024)) != - 1) { 74 str += new String(readByte).trim(); 75 } 76 return str.getBytes(); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 return null; 81 } 82 83 // Drawable转换成Bitmap 84 public Bitmap drawable2Bitmap(Drawable drawable) { 85 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 86 drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); 87 Canvas canvas = new Canvas(bitmap); 88 drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 89 drawable.draw(canvas); 90 return bitmap; 91 } 92 93 // Bitmap转换成Drawable 94 public Drawable bitmap2Drawable(Bitmap bitmap) { 95 BitmapDrawable bd = new BitmapDrawable(bitmap); 96 Drawable d = (Drawable) bd; 97 return d; 98 } 99 100 //将Bitmap转换成Base64101 public String getImgStr(Bitmap bit){102 ByteArrayOutputStream bos=new ByteArrayOutputStream();103 bit.compress(CompressFormat.JPEG, 100, bos);//参数100表示不压缩104 byte[] bytes=bos.toByteArray();105 return Base64.encodeToString(bytes, Base64.DEFAULT);106 } 107 108 //将Base64转换成bitmap109 public Bitmap getimg(String str){110 byte[] bytes;111 bytes=Base64.decode(str, 0);112 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);113 }
最近遇到比较多的转换关系,所以就整理下贴出来与大家分享咯。。。
android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。