BitMap、Drawable、inputStream及byte[] 互转

                       inputStream is=xxxx;
                  BitmapDrawable bmpDraw=new BitmapDrawable(is);
                     Bitmap bmp=bmpDraw.getBitmap();

android 中怎样将 R.drawable里面的图片资源转成换Bitmap型

Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);
Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 ); 
Canvas canvasTemp = new Canvas( newb );  
 canvasTemp.drawBitmap(bmp, 50, 50, p);




android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:

 

1、Drawable → Bitmap

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;
}

 

2、从资源中获取Bitmap

 

Resources res=getResources();

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

 

3、Bitmap → byte[]

 

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

 
4、 byte[] → Bitmap

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

 

 

 
  //将assets文件中资源取出,并将图片从bitmap转换成drawable格式
    public static Drawable getDrawableFromAssetFile(Context context,String fileName){   
        Bitmap image = null;   
        BitmapDrawable drawable=null;
        try{   
            AssetManager am = context.getAssets();   
            InputStream is = am.open(fileName);  
            image = BitmapFactory.decodeStream(is);  
            drawable= new BitmapDrawable(context.getResources(), image);
            is.close();   
        }catch(Exception e){               
        }   
        return drawable;   
    }
以上是我在实践中遇到的一些转换,以后遇到类似的就不用到处找了,希望对大家也有一点用处!



http://www.iteye.com/topic/642128
http://blog.csdn.net/scut1135/article/details/7063471android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下: 1、Drawable → Bitmap
Java代码  
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;  

}  
2、从资源中获取Bitmap 
Java代码  
Resources res=getResources();  

  

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);  
3、Bitmap → byte[] 
Java代码  
private byte[] Bitmap2Bytes(Bitmap bm){  

    ByteArrayOutputStream baos = new ByteArrayOutputStream();   

     // compress 是用于压缩图片的api接口,可以将图片压缩成jpg,png等格式。

     // 第一个参数是压缩成图片的格式

     // 第二个参数是压缩图片的质量,对于png格式,此参数可以忽略

     // 第三个参数是图片压缩成的输出流

    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    

    return baos.toByteArray();  

   }  
4、 byte[] → Bitmap
Java代码  
private Bitmap Bytes2Bimap(byte[] b){  

            if(b.length!=0){  

                return BitmapFactory.decodeByteArray(b, 0, b.length);  

            }  

            else {  

                return null;  

            }  

      }  c
以上是我在实践中遇到的一些转换,以后遇到类似的就不用到处找了,希望对大家也有一点用处!
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++android中使用BitmapFactory的decodeStream()方法解码图片失败问题
 从网络获取图片,数据为InputStream流对象,然后调用BitmapFactory的decodeStream()方法解码获取图片。代码如下: 
[java] view plaincopy

private Bitmap getUrlBitmap(String url)  

{  

Bitmap bm;  

try{  

URL imageUrl=new URL(url);  

HttpURLConnection conn=(HttpURLConnection)imageUrl.openConnection();  

conn.connect();  

InputStream is=conn.getInputStream();  

//byte[] bt=getBytes(is); //注释部分换用另外一种方式解码  

//bm=BitmapFactory.decodeByteArray(bt,0,bt.length);  

bm=BitmapFactory.decodeStream(is); //如果采用这种解码方式在低版本的API上会出现解码问题  

is.close();  

conn.disconnect();  

return bm;  

}  

catch(MalformedURLException e)  

{  

e.printStackTrace();  

}  

catch (IOException e)  

{  

e.printStackTrace();  

}  

return null;  

  

}  
结果在运行时编译器提示:          DEBUG/skia(xxx):--- decoder->decode returnedfalse 已经确定从网络获取的数据流没有出现问题,而是在图片解码时出现错误。上网查找了不少资料,也没有得出确切的原因,不过有几条意见值得关注。一种说法是在android 较低版本的api中会有不少内部的错误,我的代码运行时选择2.1API Level 7和2.2API Level 8都会出现这个问题,而选择2.3 API Level 9后能够正常解码图片。我的另外一种做法是换用别的解码方式对图片解码,见代码中被注释的那俩行,使用decodeByteArray()方法在低版本的API上也能够正常解码,解决了这个问题。其中getBytes(InputStream is)是将InputStream对象转换为Byte[]的方法,具体代码如下:
[java] view plaincopy

private byte[] getBytes(InputStream is) throws IOException {  

  

ByteArrayOutputStream baos = new ByteArrayOutputStream();  

byte[] b = new byte[1024];  

int len = 0;  

  

while ((len = is.read(b, 0, 1024)) != -1)  

{  

baos.write(b, 0, len);  

baos.flush();  

}  

byte[] bytes = baos.toByteArray();  

return bytes;  

}  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值