欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中使用 AsyncTask 异步读取网络图片

程序员文章站 2024-02-23 17:22:28
 1.新建android工程asyncloadpicture 新建布局文件activity_main.xml主界面为一个gridview,还有其子项布局文件...

 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 异步读取网络图片的相关知识,希望对大家有所帮助!