Android开发之异步下载网络图片并显示到UI界面


最近开发时遇到了打开一个界面时,需要联网下载一个网络图片,但是这个图片也不一定一成不变,索性我下载好图片,直接就在UI界面中显示出来,【也没有作缓存、图片保存在本地之类的】,也就是说每次打开这个界面都会更新新的图片。今天记录下来,供有同样简单暴力需求的朋友使用。

特别简单的例子,直接发出来代码:

异步下载图片类:

package us.cloudhawk.client.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import us.cloudhawk.client.Constants;
import us.cloudhawk.client.utils.ToolsUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class AgentLogoLoader extends AsyncTask {

	public Context context;// Context上下文
	private String path;// 图片路径
	private int width;// 图片宽度
	private int height;// 图片高度
	private onLogoDownloadListener listener;
	private static final String TAG = AgentLogoLoader.class.getSimpleName();

	/** 自定义一个接口,在需要下载图片的UI类中实现此接口 */
	public static interface onLogoDownloadListener {
		void getLogoBitmap(Bitmap bitmap);
	}

	public AgentLogoLoader(Context context, String path,
			onLogoDownloadListener listener) {
		this.context = context;
		this.path = Constants.BASE_URL + path;//http://www.baidu.com/image/dwadkwajda/134wd.jpg
		this.listener = listener;
		width = 222;
		height = 78;
	}

	@Override
	protected Bitmap doInBackground(String... params) {

		URL imageUrl = null;
		HttpURLConnection conn = null;
		Bitmap bitmap = null;
		InputStream is = null;
		BufferedInputStream bis = null;

		try {
			if (Constants.DEBUG)
				Log.d(TAG, "START TO DOWNLOAD: " + path);
			imageUrl = new URL(path);
			conn = (HttpURLConnection) imageUrl.openConnection();
			is = conn.getInputStream();
			bis = new BufferedInputStream(is);
			bitmap = BitmapFactory.decodeStream(bis);
			/* 改变头像大小 */
			//bitmap = ToolsUtil.createBitmapBySize(bitmap, width, height);

			/* 下载成功 */
			if (Constants.DEBUG)
				Log.d(TAG, "SUCCESS: " + path);
			return bitmap;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		if (listener != null) {
			listener.getLogoBitmap(result);
		}
	}

}

UI界面类:

public class test  implements OnClickListener,
		OnItemClickListener,onLogoDownloadListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_subscription_receipt);
                new AgentLogoLoader(this, mAgent.agent_logo_path, this).execute();
	}

	@Override
	public void getLogoBitmap(Bitmap bitmap) {
		if (bitmap != null) {
			BitmapDrawable drawable = new BitmapDrawable(bitmap);
			agentLogo.setImageDrawable(drawable);
			agentLogo.invalidate();
		}
	}
}


如果需要一个带缓存,更完整的异步加载工具类,我提供一个传送门,别人写的更完善一些:

http://blog.csdn.net/geniusxiaoyu/article/details/7470163



你可能感兴趣的:(Android)