联网下载图片转位图

package com.wxw.mengyuanmusic;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 下载图片的类,转成Bitmap
 */
public class LoadImageToBitmap {
    /**
     * Bitmap默认为空
     */
    private Bitmap bitmap = null;

    private OnCompletedLoadListener listener;

    /**
     * 图片下载完成的监听
     */
    public interface OnCompletedLoadListener {
        void onCompletedLoad(Bitmap bitmap);
    }

    public void setOnCompletedLoadListener(OnCompletedLoadListener listener) {
        this.listener = listener;
    }

    /**
     * 下载图片的方法
     *
     * @param imgUrl 图片的网址
     * @param method 请求图片的方式,GET 和 POST
     */
    public void loadImage(String imgUrl, String method) {

        if (method.equals("GET")) {
            doGet(imgUrl);
        } else {
            if (method.equals("POST")) {
                doPost();
            } else {
                try {
                    throw new Exception("数据请求方式只有Get和Post,请选择 GET 和 POST 两个字符串之一");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * GET请求
     *
     * @param imgUrl 图片的网址
     */
    private void doGet(final String imgUrl) {
        Thread t = new Thread() {
            @Override
            public void run() {
                // 下载来的图片的IO流
                InputStream is = null;
                HttpURLConnection conn = null;

                try {
                    URL url = new URL(imgUrl);
                    conn = (HttpURLConnection) url.openConnection();
                    // 读取超时时长
                    conn.setReadTimeout(5000);
                    // 连接超时时长
                    conn.setConnectTimeout(5000);
                    // 设置请求方式
                    conn.setRequestMethod("GET");
                    // 开始连接
                    conn.connect();
                    // 请求码
                    int code = conn.getResponseCode();

                    // 请求成功
                    if (code == HttpURLConnection.HTTP_OK) {
                        is = conn.getInputStream();

                        // 将IO流转成位图
                        bitmap = BitmapFactory.decodeStream(is);

                        if (listener != null) {
                            if (bitmap == null) {
                                throw new Exception("IO流转位图失败");
                            } else {
                                listener.onCompletedLoad(bitmap);
                            }
                        }
                    } else {
                        throw new Exception("请求图片数据失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (is != null) {
                        try {
                            // 关闭IO流
                            is.close();
                            // 关闭连接
                            conn.disconnect();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        t.start();
    }

    private void doPost() {
        Log.e("LoadImageToBitmap", "POST请求方式尚未开通");
    }
}


创建类对象,调用方法即可。记得如果在监听中加载图片,一定要使用runOnUiThread()等能使代码运行在UI线程中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值