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;
    }

你可能感兴趣的:(Android工具类)