Android工具类之图片流InputStream->(byte[])->Bitmap/Drawable

1、下载图片 (inputStream –> drawable)

/**
     * 1、下载图片 inputStream --> drawable
     *
     * @param imageUrl
     * @return
     */
    private Drawable loadDrawableImage(String imageUrl) {
        Drawable drawable = null;
        try {
            // 可以在这里通过文件名来判断,是否本地有此图片
            drawable = Drawable.createFromStream(
                    new URL(imageUrl).openStream(), "image.jpg");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawable;
    }

使用:

Drawable drawable1 = loadDrawableImage(IMAGE_URL);
imageView.setImageDrawable(drawable1);

2、getImage (inputStream –> byte –> bitmap)(最佳方法)

/**
     * 2、getImage   inputStream  --> byte  --> bitmap
     * @param path
     * @return
     * @throws Exception
     */
    public Bitmap getImage(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setReadTimeout(10 * 1000);
        conn.setConnectTimeout(10 * 1000);
        conn.setRequestMethod("GET");
        InputStream in = null;
        if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            in = conn.getInputStream();
        } else {
            in = null;
        }
        if (in == null){
            throw new RuntimeException("stream is null");
        } else {
            try {
                // 调用getBytes(in)方法
                byte[] data = getBytes(in); 
                if(data!=null){
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            in.close();
            return null;
        }
    }

得到图片字节流数组大小 (inputStream –> byte)

 /*
    * 得到图片字节流数组大小  inputStream  --> byte
    */
    public static byte[] getBytes(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1){
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inStream.close();
        return outStream.toByteArray();
    }

使用:

final Bitmap bitmap = loadBitmapImage(IMAGE_URL);
imageView.setImageBitmap(bitmap);

3、getUrlBitmap (inputStream –> bitmap) 或者(inputStream –> byte –> bitmap)

 /**
     * 2、getUrlBitmap (inputStream --> bitmap) 或者(inputStream  --> byte  --> bitmap)

     * @param url
     * @return
     */
    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;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值