使用HttpUrlConnection来加载网络图片,
投稿于 被查看 6988 次 评论:71
使用HttpUrlConnection来加载网络图片,
使用HttpUrlConnection来加载网络图片
private Context context; private Button button; private ImageView imageView; //加载网络图片需要Bitmap对象 private Bitmap bitmap; public DownloadImageTask(Context context,Button button,ImageView imageView){ this.context=context; this.button=button; this.imageView=imageView; } @Override protected Bitmap doInBackground(String... strings) { Bitmap bitmap=null; try { URL url=new URL(strings[0]); HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection(); InputStream inputStream=httpURLConnection.getInputStream(); bitmap= BitmapFactory.decodeStream(inputStream); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); imageView.setImageBitmap(bitmap); }
用户评论