android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3541字,纯文字阅读大概需要6分钟。
内容图文
![android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系](/upload/InfoBanner/zyjiaocheng/1064/aa01a4ec9e184c1381d237467f4a8999.jpg)
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()); 6return is; 7 } 8 9// 将Bitmap转换成InputStream 10public InputStream Bitmap2InputStream(Bitmap bm) { 11 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 12 bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 13 InputStream is = new ByteArrayInputStream(baos.toByteArray()); 14return is; 15 } 16 17// 将InputStream转换成Bitmap 18public Bitmap InputStream2Bitmap(InputStream is) { 19return BitmapFactory.decodeStream(is); 20 } 21 22// Drawable转换成InputStream 23public InputStream Drawable2InputStream(Drawable d) { 24 Bitmap bitmap = this.drawable2Bitmap(d); 25returnthis.Bitmap2InputStream(bitmap); 26 } 27 28// InputStream转换成Drawable 29public Drawable InputStream2Drawable(InputStream is) { 30 Bitmap bitmap = this.InputStream2Bitmap(is); 31returnthis.bitmap2Drawable(bitmap); 32 } 33 34// Drawable转换成byte[] 35publicbyte[] Drawable2Bytes(Drawable d) { 36 Bitmap bitmap = this.drawable2Bitmap(d); 37returnthis.Bitmap2Bytes(bitmap); 38 } 39 40// byte[]转换成Drawable 41public Drawable Bytes2Drawable(byte[] b) { 42 Bitmap bitmap = this.Bytes2Bitmap(b); 43returnthis.bitmap2Drawable(bitmap); 44 } 45 46// Bitmap转换成byte[] 47publicbyte[] Bitmap2Bytes(Bitmap bm) { 48 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 49 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 50return baos.toByteArray(); 51 } 52 53// byte[]转换成Bitmap 54public Bitmap Bytes2Bitmap(byte[] b) { 55if (b.length != 0) { 56return BitmapFactory.decodeByteArray(b, 0, b.length); 57 } 58returnnull; 59 } 60 61// 将byte[]转换成InputStream 62public InputStream Byte2InputStream(byte[] b) { 63 ByteArrayInputStream bais = new ByteArrayInputStream(b); 64return bais; 65 } 66 67// 将InputStream转换成byte[] 68publicbyte[] InputStream2Bytes(InputStream is) { 69 String str = ""; 70byte[] readByte = newbyte[1024]; 71int readCount = - 1; 72try { 73while ((readCount = is.read(readByte, 0, 1024)) != - 1) { 74 str += new String(readByte).trim(); 75 } 76return str.getBytes(); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80returnnull; 81 } 82 83// Drawable转换成Bitmap 84public 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); 90return bitmap; 91 } 92 93// Bitmap转换成Drawable 94public Drawable bitmap2Drawable(Bitmap bitmap) { 95 BitmapDrawable bd = new BitmapDrawable(bitmap); 96 Drawable d = (Drawable) bd; 97return d; 98 } 99100//将Bitmap转换成Base64101public String getImgStr(Bitmap bit){ 102 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 103 bit.compress(CompressFormat.JPEG, 100, bos);//参数100表示不压缩104byte[] bytes=bos.toByteArray(); 105return Base64.encodeToString(bytes, Base64.DEFAULT); 106 } 107108//将Base64转换成bitmap109public Bitmap getimg(String str){ 110byte[] bytes; 111 bytes=Base64.decode(str, 0); 112return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 113 }
最近遇到比较多的转换关系,所以就整理下贴出来与大家分享咯。。。
原文:http://www.cnblogs.com/yrhua/p/3946196.html
内容总结
以上是互联网集市为您收集整理的android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系全部内容,希望文章能够帮你解决android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。