HttpURLConnection 获取图片

来源:互联网 发布:如何提升淘宝搜索排名 编辑:程序博客网 时间:2024/06/01 08:59
private class MyRunnable implements Runnable {    private final String imageUrl;    private final int position;    public MyRunnable(String imageUrl, int position) {        this.imageUrl = imageUrl;        this.position = position;    }    @Override    public void run() {        try {            URL url = new URL(imageUrl);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setConnectTimeout(4000);            connection.setReadTimeout(4000);            connection.connect();            if (connection.getResponseCode() == 200) {                InputStream is = connection.getInputStream();                Bitmap bitmap = BitmapFactory.decodeStream(is);                Message msg = Message.obtain();                msg.what = SUCCESS;                msg.arg1 = position;                msg.obj = bitmap;                handler.sendMessage(msg);                Log.e("success", "成功");                localCacheUtils.putBitmapFromUrl(imageUrl,bitmap);            }        } catch (Exception e) {            Log.e("fail", "失败" + e.toString());        }    }}
原创粉丝点击