XX转Bitmap,XX转Drawable,Bitmap和base64转换

图片存储路径/Base64互相转换 

(1)根据图片存储路径转换成base64

/**
     * 将图片转换成Base64编码的字符串
     *
     * @param imgPath 图片存储的路径
     */
    public static String imageToBase64(String imgPath) {
        if (TextUtils.isEmpty(imgPath)) {
            return null;
        }
        InputStream is = null;
        byte[] data;
        String result = null;
        try {
            is = new FileInputStream(imgPath);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.NO_CLOSE);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

(2)将图片base64存储到本地图片

/**
 * 将Base64编码转换为图片
 * @param base64Str
 * @param path
 * @return true
 */
public static boolean base64ToFile(String base64Str,String path) {
    byte[] data = Base64.decode(base64Str,Base64.NO_WRAP);
    for (int i = 0; i < data.length; i++) {
        if(data[i] < 0){
            //调整异常数据
            data[i] += 256;
        }
    }
    OutputStream os = null;
    try {
        os = new FileOutputStream(path);
        os.write(data);
        os.flush();
        os.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }catch (IOException e){
        e.printStackTrace();
        return false;
    }
}

一、转成Bitmap

(1)rgb转bitmap

private Bitmap createColorBitmap(String rgb, int width, int height) {

        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        int color = Color.parseColor(rgb);

        bmp.eraseColor(color);

        return bmp;

    }
    //Usage
    Bitmap bmp = createColorBitmap("#cce8cf", 200, 50);

(2)int型颜色转bitmap

private Bitmap createColorBitmap(int color, int width, int height) {

        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        bmp.eraseColor(color);

        return bmp;

    }

//Usage

    Bitmap bmp = createColorBitmap(Color.BLUE, 200, 50);

(3)字节数组转bitmap

private Bitmap getBitmapFromByteArray(byte[] array) {

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

    }

(4)读取文件转bitmap

private Bitmap getBitmapFromFile(String pathName) {

        return BitmapFactory.decodeFile(pathName);

    }

(5)读取资源转bitmap

private Bitmap getBitmapFromResource(Resources res, int resId) {

        return BitmapFactory.decodeResource(res, resId);

    }

(6)输入流转bitmap

private Bitmap getBitmapFromStream(InputStream inputStream) {

        return BitmapFactory.decodeStream(inputStream);

    }

(7)Drawable转bitmap(AndroidStudio引用图片为:R.mipmap.ic_launcher)

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);


二、转成Drawable

(1)资源文件转Drawable

Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);

(2)Bitmap转Drawable

 Drawable d = new BitmapDrawable(getResources(),bitmap);

三、Bitmap和base64转换

(1)Bitmap转base64

    /** 
     * bitmap转为base64 
     * @param bitmap 
     * @return 
     */  
    public static String bitmapToBase64(Bitmap bitmap) {  
      
        String result = null;  
        ByteArrayOutputStream baos = null;  
        try {  
            if (bitmap != null) {  
                baos = new ByteArrayOutputStream();  
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
      
                baos.flush();  
                baos.close();  
      
                byte[] bitmapBytes = baos.toByteArray();  
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (baos != null) {  
                    baos.flush();  
                    baos.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return result;  
    }  

(2)base64转Bitmap


    /** 
     * base64转为bitmap 
     * @param base64Data 
     * @return 
     */  
    public static Bitmap base64ToBitmap(String base64Data) {  
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);  
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值