1. New Android Project Asyncloadpicture
New layout file Activity_main.xml The main interface is a GridView and its child layout file Gridview_item.xml
2. Function main interface Mainactivity.java, main code is as follows
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 Arrayli
st<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.dec
Oderesource (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); /** picture Asynchronous Load class/public class Imageloadtask extends Asynctask<string, Void, void> {private Imageadapter adapter; pu Blic Imageloadtask (Context context, Imageadapter adapter) {This.adapter = adapter;} @Override protected Void Doinbackgro und (String ... params) {string url = params[];//Gets the parameter, picture URI address String p = params[]; for (int i =; I < Adapter.getcou NT ();
i++) {imageentity bean = (imageentity) adapter.getitem (i); Bitmap Bitmap = Bitmapfactory.decodestream (Request.
Handlerdata (URL));
Bean.setimage (bitmap); Publishprogress ();
Notify to update UI} return null;
} public void onprogressupdate (void ... voids) {if (iscancelled ()) return;//update UI adapter.notifydatasetchanged ();} /** Picture Entity */public class Imageentity {Private Bitmap image, public Bitmap getImage () {return image;} public void Setima
GE (Bitmap image) {this.image = image;}} }
3.GridView list Picture Adapter code 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--Picture Adapter/public class Imageadapter extends Baseadapter {private context _context; private
Arraylist<imageentity> _list; Public Imageadapter (context context, arraylist<imageentity> imageList) {this._context = context; this._list = Imag
Elist; @Override public int GetCount () {return _list.size ():} @Override public Object getitem (int position) {return _list.ge
T (position); @Override public long getitemid (int position) {return position;} @Override public view getview (int position, View conv Ertview, 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 a picture is read, no skips over if (b.getimage ()!= null) {Imgview.setimagebitmap (B.getimage ()); }
}
4. Request a network to read a picture class 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; /** * Tool class, mainly according to the URL to read the picture return stream method * * @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;
/** directly returns drawable object settings picture/public static drawable Loadimagefromnetwork (String imageUrl) {drawable drawable = null; try {
You can use the filename here to determine whether there is a local picture 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; }
}
The above content to introduce the android in the use of Asynctask asynchronous reading Network Pictures related knowledge, I hope to help!