import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.widget.ImageView; public class DownLoadImage extends AsyncTask<String, Void, Bitmap> { ImageView imageView; public DownLoadImage(ImageView imageView) { // TODO Auto-generated constructor stub this.imageView = imageView; } @Override protected Bitmap doInBackground(String... urls) { // TODO Auto-generated method stub String url = urls[0]; Bitmap tmpBitmap = null; try { InputStream is = new java.net.URL(url).openStream(); tmpBitmap = BitmapFactory.decodeStream(is); } catch (Exception e) { e.printStackTrace(); } return tmpBitmap; } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub imageView.setImageBitmap(result); } }
写一个这样的工具类,然后用的时候很简单
DownLoadImage t=new DownLoadImage(imgview);
t.execute(imgurl);
如果大家谁有更好的办法 欢迎讨论交流。
时间: 2024-10-11 17:22:39