Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 中使用 AsyncTask 異步讀取網絡圖片

Android 中使用 AsyncTask 異步讀取網絡圖片

編輯:關於Android編程

 1.新建Android工程AsyncLoadPicture

新建布局文件activity_main.xml主界面為一個GridView,還有其子項布局文件gridview_item.xml

2.功能主界面MainActivity.java,主代碼如下

package com.example.asyncloadpicture;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.GridView;
/**
* @author ymw
* @data --
*/
public class MainActivity extends Activity {
private GridView gridViewImg;
private ArrayList<ImageEntity> imageList = new ArrayList<ImageEntity>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridViewImg = (GridView) findViewById(R.id.gridViewImg);
initData();
}
private void initData() {
for (int i = ; i < ; i++) {
ImageEntity b = new ImageEntity();
b.setImage(BitmapFactory.decodeResource(getResources(),
R.drawable.nopicture));
imageList.add(b);
}
ImageAdapter adapter = new ImageAdapter(getApplicationContext(),
imageList);
gridViewImg.setAdapter(adapter);
String[] params = {
"http://images.cnitblog.com/i///.png",
"p" };
new ImageLoadTask(getApplicationContext(), adapter).execute(params);
}
/** 圖片異步加載類 */
public class ImageLoadTask extends AsyncTask<String, Void, Void> {
private ImageAdapter adapter;
public ImageLoadTask(Context context, ImageAdapter adapter) {
this.adapter = adapter;
}
@Override
protected Void doInBackground(String... params) {
String url = params[];// 獲取傳來的參數,圖片uri地址
String p = params[];
for (int i = ; i < adapter.getCount(); i++) {
ImageEntity bean = (ImageEntity) adapter.getItem(i);
Bitmap bitmap = BitmapFactory.decodeStream(Request
.HandlerData(url));
bean.setImage(bitmap);
publishProgress(); // 通知去更新UI
}
return null;
}
public void onProgressUpdate(Void... voids) {
if (isCancelled())
return;
// 更新UI
adapter.notifyDataSetChanged();
}
}
/** 圖片實體 */
public class ImageEntity {
private Bitmap image;
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
}
}

3.GridView列表圖片適配器代碼ImageAdapter.java

package com.example.asyncloadpicture;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.example.asyncloadpicture.MainActivity.ImageEntity;
/**
* 
* @author ymw
* @data -- 圖片適配器
*/
public class ImageAdapter extends BaseAdapter {
private Context _context;
private ArrayList<ImageEntity> _list;
public ImageAdapter(Context context, ArrayList<ImageEntity> imageList) {
this._context = context;
this._list = imageList;
}
@Override
public int getCount() {
return _list.size();
}
@Override
public Object getItem(int position) {
return _list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(R.layout.gridview_item, null);
ImageView imgView = (ImageView) convertView.findViewById(R.id.imgView);
ImageEntity b = _list.get(position);
// 如果有圖片則讀取,沒有則跳過
if (b.getImage() != null) {
imgView.setImageBitmap(b.getImage());
}
return convertView;
}
}

4.請求網絡讀取圖片的類Request.java

package com.example.asyncloadpicture;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.drawable.Drawable;
import android.util.Log;
/**
* 工具類,主要根據url讀取圖片返回流的方法
* 
* @author ymw
* 
*/
public class Request {
public static InputStream HandlerData(String url) {
InputStream inStream = null;
try {
URL feedUrl = new URL(url);
URLConnection conn = feedUrl.openConnection();
conn.setConnectTimeout( * );
inStream = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inStream;
}
/** 直接返回Drawable對象設置圖片 */
public static Drawable loadImageFromNetwork(String imageUrl) {
Drawable drawable = null;
try {
// 可以在這裡通過文件名來判斷,是否本地有此圖片
drawable = Drawable.createFromStream(
new URL(imageUrl).openStream(), "image.jpg");
} catch (IOException e) {
Log.d("test", e.getMessage());
}
if (drawable == null) {
Log.d("test", "null drawable");
} else {
Log.d("test", "not null drawable");
}
return drawable;
}
}

以上內容給大家介紹了Android 中使用 AsyncTask 異步讀取網絡圖片的相關知識,希望對大家有所幫助!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved