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()); } } }
HttpURLConnection 获取图片
最新推荐文章于 2025-06-11 13:06:20 发布