android开发路-bitmap转缓存输入流BufferedInputStream

我们往服务器上传图片时往往要对一个图片对象进行输入输出流的转化

ByteArrayOutputStream baos = new ByteArrayOutputStream();
//compress方法是把一个位图写到一个OutputStream中,参数一是位图对象,二是格式,三是压缩的质量,四是输出流
newimage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//输出流转成输入流
InputStream inputimage = new ByteArrayInputStream(baos.toByteArray());
final BufferedInputStream inputImage = new BufferedInputStream(inputimage)


Drawable转Bitmap的两种方法:

一:

private Bitmap bitmap;
private void drawableToBitamp(Drawable drawable)
    {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        System.out.println("Drawable转Bitmap");
        Bitmap.Config config = 
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565;
        bitmap = Bitmap.createBitmap(w,h,config);
        //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(bitmap);   
        drawable.setBounds(0, 0, w, h);   
        drawable.draw(canvas);
    }
二:
private Bitmap bitmap;

private void drawableToBitamp(Drawable drawable)
    {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        bitmap = bd.getBitmap();
    }


你可能感兴趣的:(android开发点滴)