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 → Bitmappublic 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[] → Bitmapprivate 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代码
publicstaticBitmapdrawableToBitmap(Drawabledrawable){



Bitmapbitmap=Bitmap

.createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888

:Bitmap.Config.RGB_565);

Canvascanvas=newCanvas(bitmap);

//canvas.setBitmap(bitmap);

drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

drawable.draw(canvas);

returnbitmap;

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



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

ByteArrayOutputStreambaos=newByteArrayOutputStream();

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

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

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

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

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

returnbaos.toByteArray();

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

if(b.length!=0){

returnBitmapFactory.decodeByteArray(b,0,b.length);

}

else{

returnnull;

}

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

privateBitmapgetUrlBitmap(Stringurl)

{

Bitmapbm;

try{

URLimageUrl=newURL(url);

HttpURLConnectionconn=(HttpURLConnection)imageUrl.openConnection();

conn.connect();

InputStreamis=conn.getInputStream();

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

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

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

is.close();

conn.disconnect();

returnbm;

}

catch(MalformedURLExceptione)

{

e.printStackTrace();

}

catch(IOExceptione)

{

e.printStackTrace();

}

returnnull;



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

privatebyte[]getBytes(InputStreamis)throwsIOException{



ByteArrayOutputStreambaos=newByteArrayOutputStream();

byte[]b=newbyte[1024];

intlen=0;



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

{

baos.write(b,0,len);

baos.flush();

}

byte[]bytes=baos.toByteArray();

returnbytes;

}



更多相关文章

  1. Android(安卓)之 自定义控件用法介绍
  2. AsyncTaskLoader使用方法
  3. Android单元测试笔记
  4. 三行Android代码实现白天夜间模式流畅切换
  5. Android(安卓)一键锁屏实现
  6. Android两级嵌套ListView滑动问题的解决
  7. S5PV210 Android(安卓)分支代码学习
  8. android中设置AlertDialog的大小
  9. Android中Fragment中启动一个Activity,实例化控件。

随机推荐

  1. Guava学习笔记(1)--安全地使用null(Using
  2. 关于java的wait()的问题
  3. JAVA复习3 Java类和对象
  4. java使用freemarker导出复杂的excel表格
  5. 小日志与大日志:通过Java进行日志记录
  6. spring cloud(学习笔记)高可用注册中心(Eure
  7. 使用SWT/JFace与WindowBuilder绑定数据的
  8. java SSH员工管理系统以及Demo代码下载
  9. 做一个没有视图的零食吧?
  10. Spring Security的permitAll对某些端点不