: Import Java.io.File; Import Java.io.FileOutputStream; Import java.io.IOException; Import Java.io.InputStream; Import Java.io.OutputStream; Import Java.net.URL; Import java.net.URLConnection; Import Android.content.Context; Import Android.graphics.Bitmap; Import Android.graphics.BitmapFactory; Import android.graphics.drawable.Drawable; Import Android.net.Uri; Import Android.os.AsyncTask; Import Android.util.AttributeSet; Import Android.widget.ImageView; /** * Asynchronous Picture control * Use: New Asyncimageview (). Asyncloadbitmapfromurl ("Http://xxxx", "cache path") { * * @author gaoomei@gmail.com * @site http://obatu.sinaapp.com * @version 1.0 * @2011-12-3 */ public class Asyncimageview extends ImageView { /** * Asynchronous Task Loader */ Private Asyncloadimage masyncload; /** * Download back picture cache live time, unit: seconds (s), default 30 minutes */ Private long mcachelivetime = 1800; Public Asyncimageview { Super (context); } Public Asyncimageview (context context, AttributeSet Attrs) { Super (context, attrs); } Public Asyncimageview (context context, AttributeSet attrs, int defstyle) { Super (context, attrs, Defstyle); } /** * */ @Override public void setimagedrawable (drawable drawable) { if (masyncload!= null) { Masyncload.cancel (TRUE); Masyncload = null; } Super.setimagedrawable (drawable); } /** * Rewrite the following methods to set the picture resource, which is to cancel the network load */ @Override public void Setimageresource (int resid) { Cancelload (); Super.setimageresource (RESID); } @Override public void Setimageuri (Uri uri) { Cancelload (); Super.setimageuri (URI); } @Override public void Setimagebitmap (Bitmap Bitmap) { Cancelload (); Super.setimagebitmap (bitmap); } /** * Cancel an asynchronous task in progress */ public void Cancelload () { if (masyncload!= null) { Masyncload.cancel (TRUE); Masyncload = null; } } /** * Set Picture Survival time * * @param second * Survival time, unit "seconds", if equal to 0 or null, do not cache */ public void Setcachelivetime (long second) { if (second = = 0) { This.mcachelivetime = 0; else if (second >= 0) { This.mcachelivetime = second * 1000; } } /** * Asynchronous loading from the network * * @param URL * @param savefilename */ public void Asyncloadbitmapfromurl (string url, string savefilename) { if (masyncload!= null) { Masyncload.cancel (TRUE); } Asynctask is not reusable, so each time a new instance Masyncload = new Asyncloadimage (); Masyncload.execute (URL, savefilename); } /** * Asynchronous Loader */ Private class Asyncloadimage extends Asynctask { /** * Whether to cancel */ Private Boolean iscancel = false; @Override Protected Bitmap doinbackground (String ... params) { if (iscancel) { return null; } String URL = params[0]; String fileName = params[1]; try { Return Getbitmap (URL, fileName); catch (IOException e) { E.printstacktrace (); } return null; } @Override protected void oncancelled () { System.out.println ("Async load Imgae Cancel"); Iscancel = true; } @Override protected void OnPostExecute (Bitmap result) { if (!iscancel && result!= null) { AsyncImageView.this.setImageBitmap (result); } } } /** * Download pictures * * @param urlstring * URL Download Address * @param fileName * Cache file path * @throws IOException */ Private Bitmap Getbitmap (String urlstring, String fileName) Throws IOException { if (FileName = = NULL | | Filename.trim (). IsEmpty ()) { InputStream input = Getbitmapinputstreamfromurl (urlstring); return Bitmapfactory.decodestream (input); } file file = new file (fileName); if (!file.isfile () | | (Mcachelivetime > 0 && (System.currenttimemillis () - File.lastmodified () > Mcachelivetime)) { inputstream input = Getbitmapinputstreamfromurl ( URLString); file = saveimage (input, fileName); //If the file structure creation fails, decode the picture directly from the input stream if (file = = NULL | | |!file.exists () | |! File.canwrite () | |!file.canread ()) { return Bitmapfactory.decodestream (input); } } return Bitmapfactory.decodefile (File.getabsolutepath ()); } /** * Download pictures, enter InputStream * * @param urlstring * @return * @throws IOException */ Private InputStream Getbitmapinputstreamfromurl (String urlstring) Throws IOException { URL url = new URL (urlstring); URLConnection connection = Url.openconnection (); Connection.setconnecttimeout (25000); Connection.setreadtimeout (90000); return Connection.getinputstream (); } /** * Save pictures from input stream to file system * * @param fileName * @param input * @return */ Private File saveimage (InputStream input, String fileName) { if (Filename.trim (). IsEmpty () | | input = = NULL) { return null; } File File = new file (fileName); OutputStream output = null; try { File.getparentfile (). Mkdirs (); if (file.exists () && file.isfile ()) { File.delete (); } if (!file.createnewfile ()) { return null; } Output = new FileOutputStream (file); byte[] buffer = new BYTE[4 * 1024]; do { Looping read int numread = input.read (buffer); if (Numread = = 1) { Break } Output.write (buffer, 0, numread); } while (true); Output.flush (); catch (Exception e) { E.printstacktrace (); finally { try { Output.close (); catch (IOException e) { E.printstacktrace (); catch (Exception E2) { E2.printstacktrace (); } } return file; } } |