android中表示图片的Bitmap、Drawable之间的转化以及他们和字节数组、输入流之间的转化

package com.android.music;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;


public class PictureConvertTypeUtil {
    
    //Bitmap转换为Drawable
    public static Drawable bitmapToDrawable(Bitmap bitmap){
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
        Drawable drawable = (Drawable)bitmapDrawable;
        return drawable;
    }
    
    //Drawable转换为Bitmap
    public static Bitmap drawableToBitmap(Drawable drawable){
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();
        Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, config);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawableWidth, drawableHeight);
        drawable.draw(canvas);
        return bitmap;
    }
    
    //Bitmap转换成字节数组
    public static byte[] bitmapToByteArray(Bitmap bitmap){
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100 , outputStream);
        return outputStream.toByteArray();
    }
    
    //字节数组转换成Bitmap
    public static Bitmap byteArrayToBitmap(byte[] bytes){
        if(bytes.length > 0){
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        }
        return null;
    }
    
    //Drawable转化成字节数组
    public static byte[] drawableTobyteArray(Drawable drawable){
        Bitmap bitmap = drawableToBitmap(drawable);
        return bitmapToByteArray(bitmap);
    }
    
    //字节数组转化成Drawable
    public static Drawable byteArrayToDrawable(byte[] bytes){
        Bitmap bitmap = byteArrayToBitmap(bytes);
        Drawable drawable = bitmapToDrawable(bitmap);
        return drawable;
    }
    
    //将字节数组转化成InputStream
    public static InputStream byteArrayToInputStream(byte[] bytes){
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        return bais;
    }
    
    //InputStream转化成字节数组
    public static byte[] inputStreamToByteArray(InputStream inputStream){
        String tempString = "";
        byte[] readBytes = new byte[1024];
        int readLength = -1;
        try {
            while (inputStream.read(readBytes, 0, 1024) != -1) {
                tempString += new String(readBytes).trim();
            }
            return tempString.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    //Bitmap转换成InputStream
    public static InputStream bitmapToInputStream(Bitmap bitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        return bais;
    }
    
    //将InputStream转化成Bitmap
    public static Bitmap inputStreamToBitmap(InputStream inputStream){
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        return bitmap;
    }
    
    //Drawable转化成InputStream
    public static InputStream drawableToInputStream(Drawable drawable){
        Bitmap bitmap = drawableToBitmap(drawable);
        InputStream inputStream = bitmapToInputStream(bitmap);
        return inputStream;
    }
    
    //InputStream转化成Drawable
    public static Drawable inputStreamToDrawable(InputStream inputStream){
        Bitmap bitmap = inputStreamToBitmap(inputStream);
        Drawable drawable = bitmapToDrawable(bitmap);
        return drawable;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值