One, load a picture
Vo Lley is through imagerequest to get the picture on the network, specify a URL, and return an encoded number of the bitmap. Of course, it also provides other convenience features, such as resizing pictures. The main benefit of using it is that volley 's plan thread ensures that expensive operations such as chip encoding, resizing, and so on are automatically completed on a single worker thread, without causing too much trouble and distraction to the main thread.
A cannedrequest for getting an image at a given URL and calling back with a decodedbitmap. It also provides convenience features like specifying a size to Resizeto. Its main benefit are that volley's thread scheduling ensures that expensiveimage operations (decoding, resizing) Automatica Lly happen on a worker thread.
( 1 ) initialize a request queue preferably written in application manifest edit application
public class Volleyapplication extends application {@Overridepublic void OnCreate () {super.oncreate (); Requestmanager.initvolley (this);}}
The requestmanager here is my custom request management class, specifically for managing requests. There is a request queue, which is then instantiated in the initialization method.
private static Requestqueue Mrequestqueue;public Requestmanager () {//TODO auto-generated constructor stub} public static void Initvolley (Context context) {Mrequestqueue = Volley.newrequestqueue (context);}
( 2 ) and then write the picture request
Retrieves an image specified by the URL, displays it in the UI. Request = new Imagerequest (URL, new response.listener<bitmap> () { @Override public void Onresponse ( Bitmap response) { Singleimageview.setimagebitmap (response); } }, 0, 0, NULL, new Response.errorlistener () {public void Onerrorresponse (volleyerror error) { Toast.maketext ( Imageactivity.this, "Download image error", Toast.length_long). Show (); } })
( 3 ) to execute the request to load the image:
Requestmanager.addrequest (Request);
( 4 ) in Manifest permission to add Network access in the.
<uses-permission android:name= "Android.permission.INTERNET"/>
When you click on the download of a large picture, an instant print below the memory shortage warning.
Therefore, multiple images can not be downloaded multiple times the last operation. have to use Imageloader. See 2nd
Two, more pictures to download
by instantiating a imagerequest can be downloaded to a picture, the question is if more than one picture? Do you still play like that? You can see that there is not enough memory, so more than one will inevitably appear OOM. As a result,volley provides imageloader and Networkimageview to download a large number of images, such as a listview the.
( 1 ) to establish a memory cache, Android provides a memory cache class library, LruCache Note that there is also a class that provides lrucache, but what we need is Android.support.v4.util. LruCache
New class inherits from Lr ucache Imagecache interface.
Import Android.graphics.bitmap;import Android.support.v4.util.lrucache;import Com.android.volley.toolbox.imageloader;public class Bitmaplrucache extends Lrucache<string, Bitmap> implements Imageloader.imagecache{public bitmaplrucache (int maxSize) {super (maxSize);} @Overrideprotected int sizeOf (String key, Bitmap Bitmap) {return bitmap.getrowbytes () * Bitmap.getheight ();} @Overridepublic Bitmap getbitmap (String URL) {return g ET (URL);} @Overridepublic void Putbitmap (String url, Bitmap Bitmap) {put (URL, Bitmap);}}
( 2 ) Instantiation Imageloader
Android The amount of memory allocated by the system to each application is different. by Runtime.getruntime (). MaxMemory () or
((Activitymanager) Context.getsystemservice (Context.activity_service))
. Getmemoryclass ();
The specific maximum amount of memory that can be allocated. Note that two units are not the same.
private static Imageloader Mimageloader; Get the app's available memory given by System,note, it unit is MB int memclass = ((Activitymanager) context.ge Tsystemservice (Context.activity_service)). Getmemoryclass (); Or you can get the memory value by This//memclass = (int) runtime.getruntime (). MaxMemory ();//Use 1/8th of the AVAILABL E memory for this memory cache.int cacheSize = 1024x768 * 1024x768 * Memclass/4;mimageloader = new Imageloader (Mrequestqueue, new Bitmaplrucache (cacheSize)); /** * Get imageloader */public static Imageloader Getimageloader () {if (Mimageloader! = null) {return mimageloader;} Else{throw New IllegalStateException ("The Imageloader is null and not ' be initialized!");}}
(3) inAdapterthrough theNetworkimageviewof theSetimageurlmethod to loadImageloaderDownloadURLthe specified picture.
HOLDER.SHOWIMAGE.SETIMAGEURL (Imagearraylist.get (position), imageloader);
Note that this is due to the ordinary ImageView not used in Image Loader method, so you must use the Volley the development of inherit from ImageView sub-class Networkimageview to use.
As follows:
Test Demo Download
Figure out the volley of the Android network library ListView loads a lot of pictures