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<String, Integer, Bitmap> {

	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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章