public class CanvasImageTaskCall extends AsyncTask<ImageView, Void, Bitmap> implements Callback{ private ImageView gView ; protected Bitmap doInBackground(ImageView... views) { Bitmap bmp = null ; ImageView view = views[0]; // 根据iconUrl获取图片并渲染,iconUrl的url放在了view的tag中。 if (view.getTag() != null) { try { URL url = new URL(view.getTag().toString()); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream stream = conn.getInputStream(); bmp = BitmapFactory.decodeStream(stream); stream.close(); } catch (Exception e) { e.printStackTrace(); Log.v("img", e.getMessage()); Message msg = new Message(); msg.what = 0; handleMessage(msg); return null; } } this.gView = view; return bmp; } protected void onPostExecute(Bitmap bm) { if (bm != null) { this.gView.setImageBitmap(bm); this.gView.setTag(bm); this.gView = null ; Message msg = new Message(); msg.what = 1; handleMessage(msg); } } public boolean handleMessage(Message msg) { // TODO Auto-generated method stub return false; } }
在 Activity 中直接调用