【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/

Drawable互转Bitmap

Drawable转Bitmap

1234
Resources res = getResources();Drawable drawable = res.getDrawable(R.drawable.myimage);BitmapDrawable bd = (BitmapDrawable) d;Bitmap bm = bd.getBitmap();
123456789101112
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;}

Bitmap转Drawable

12345678
Bitmap bm=xxx; //xxx根据你的情况获取BitmapDrawable bd=BitmapDrawable(bm);BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

mPicPath//本地图片路径转成Bitmap格式Bitmap pic = BitmapFactory.decodeFile(this.mPicPath);		image.setImageBitmap(pic);转成Bitmap格式

String与InputStream相互转换

String to InputStream

123
String str = "String与InputStream相互转换";InputStream   in_nocode   =   new   ByteArrayInputStream(str.getBytes());   InputStream   in_withcode   =   new   ByteArrayInputStream(str.getBytes("UTF-8"));

InputStream to String

这里提供几个方法。

方法1:

123456789101112131415161718192021
public String convertStreamToString(InputStream is) {    BufferedReader reader = new BufferedReader(new InputStreamReader(is));         StringBuilder sb = new StringBuilder();   

      String line = null;         try {             while ((line = reader.readLine()) != null) {                 sb.append(line + "/n");             }         } catch (IOException e) {             e.printStackTrace();         } finally {             try {                 is.close();             } catch (IOException e) {                 e.printStackTrace();             }         }   

      return sb.toString();     }

方法2:

12345678
public   String   inputStream2String   (InputStream   in)   throws   IOException   {         StringBuffer   out   =   new   StringBuffer();         byte[]   b   =   new   byte[4096];         for   (int   n;   (n   =   in.read(b))   !=   -1;)   {                 out.append(new   String(b,   0,   n));         }         return   out.toString(); }

方法3:

12345678
public   static   String   inputStream2String(InputStream   is)   throws   IOException{         ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream();         int   i=-1;         while((i=is.read())!=-1){         baos.write(i);         }        return   baos.toString(); }

Bitmap 和 byte[]互转

Bitmap → byte[]

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

byte[] → Bitmap

12345678
private Bitmap Bytes2Bimap(byte[] b){                 if(b.length!=0){                         return BitmapFactory.decodeByteArray(b, 0, b.length);                 }                 else {                         return null;                 }       }
时间: 2024-08-07 03:58:47

【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]的相关文章

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 InputSt

android 将图片通过base64转换为String 将图片String转换为Bitmap

1.Bitmap转换为图片字符串 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //该方法用来压缩图片,第一个参数为图片格式,第二个参数为截取图片的保留率,如当前为90,则保留之前图片90%的区域 bitmap.compress(Bitmap

Android基础之R.drawable.***生成Drawa与Bitmap

R.drawable.***中的文件是我们常用到的,不过有时候又没有直接的方法通过R文件获得图片,这时候就需要我们直接来转换一下了 下面提供四种方法给大家参考: 1. Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawable(R.drawable.***); 2. Resources resources  = this.getContext().getResources();

Android Drawable 和String 相互转化

在我们经常应用开发中,经常用到将drawable和string相互转化.注意这情况最好用于小图片入icon等. [java] view plain copy public synchronized Drawable byteToDrawable(String icon) { byte[] img=Base64.decode(icon.getBytes(), Base64.DEFAULT); Bitmap bitmap; if (img != null) { bitmap = BitmapFact

<BitMap>大名鼎鼎的bitmap算法

BitMap 抛砖引玉 首先,我们思考一个问题:如何在3亿个整数(0~2亿)中判断某一个数是否存在?现在只有一台机器,内存只有500M 这个问题像不像我们之前提到过的一个在0-10个数中,判断某一个数是否存在的问题呢?当时我们采取的做法是,建立一个长度是11的数组,下标从0开始,如果1存在则data[1] = 1,数字作为数组的下标,若该数字存在则在data[数字] = 1,将其赋值为1.那么我们这个是否可以这么做呢? 明显不行.为什么呢?因为我们如果判断2亿个数字是否存在,建立一个2亿长度的数

java常用string inputStream转换

1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes()); 2.InputStream–>String inputStream input; StringBuffer out = new StringBuffer(); byte[] b = ne

C# string类型和byte[]类型相互转换

string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: string str = System.Text.Encoding.Default.GetString ( byteArray ); string类型转成ASCII byte[]: ("01" 转成 byte[] = new byte[]{ 0x30,0x31}) byte[] byteA

基于java的InputStream.read(byte[] b,int off,int len)算法学习

public int read(byte[] b, int off, int len) throws IOException 将输入流中最多 len 个数据字节读入字节数组.尝试读取多达 len 字节,但可能读取较少数量.以整数形式返回实际读取的字节数. 在输入数据可用.检测到流的末尾或者抛出异常前,此方法一直阻塞. 如果 b 为 null,则抛出 NullPointerException. 如果 off 为负,或 len 为负,或 off+len 大于数组 b 的长度,则抛出 IndexOut

关于bitmap recycle trying to use a recycled bitmap android.graphics.Bitmap

在开发中,一直使用4.0以上手机作为测试机所以一直没有出现这个问题,今天换了2.3版本的手机,出现了这个错误: trying to use a recycled bitmap android.graphics.Bitmap 后检查代码,我的图片回收代码是介个样子的: public static void recycle(View view) { if (null == view) { return; } BitmapDrawable bd = (BitmapDrawable) view.getB