Today work needs to write a reserved network image cache class, and share with you
In fact, the principle of implementation is very simple, that is, downloading image data from the Internet, while the data into drawable and loaded into the specified ImageView
Save As Download_image.jpg, open the picture with intent when you click ImageView
I've written the process of working with pictures as classes
Package Com.example.downloadandopenimage;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import java.net.HttpURLConnection; Import Java.net.url;import Android.content.context;import Android.content.intent;import Android.graphics.drawable.drawable;import Android.net.uri;import Android.os.asynctask;import Android.os.environment;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.progressbar;public class Loadandsaveimage {ImageView view; Context Mcontext;public Loadandsaveimage () {//TODO auto-generated Constructor stub}public loadandsaveimage (context Context, String str, ImageView view) {//TODO auto-generated constructor Stubthis.view = View;mcontext = Context;new Load_ And_save_image (). Execute (str);} void release () {File File = new file ("file://" + environment.getexternalstoragedirectory (). toString () + "/download_ Image.jpg "); if (file.exists()) {Boolean deleted = File.delete ();}} Class Load_and_save_image extends asynctask<string, void, void> {@Overrideprotected void Doinbackground (String ... surl) {//TODO auto-generated method Stubinputstream input = Null;outputstream output = null; HttpURLConnection connection = null;try {URL url = new URL (surl[0]); connection = (HttpURLConnection) url.openconnection () ; Connection.connect ();//Download the Fileinput = Connection.getinputstream (); File Checker = new file ("file://" + environment.getexternalstoragedirectory (). toString () + "/download_image.jpg"); if ( Checker.exists ()) {Boolean deleted = Checker.delete ();} Output = new FileOutputStream (Environment.getexternalstoragedirectory (). toString () + "/download_image.jpg"); data[] = new Byte[4096];int count;while ((count = input.read (data))! =-1) {output.write (data, 0, count);}} catch (Exception e) {} finally {try {if (input! = null) input.close (); if (output! = null) {Output.close ();}} catch (Ioexcep tion ignored) {}if (Connection! = Null) Connection.disconnect ();} return null;} protected void OnPostExecute (void result) {drawable d = drawable.createfrompath ( Environment.getexternalstoragedirectory (). toString () + "/download_image.jpg"); View.setimagedrawable (d); View.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubintent Intent = new Intent (); intent.setaction (Intent.action_view); Intent.addflags (intent.flag_activity_new_ TASK); Intent.setdataandtype (Uri.parse ("file://" + environment.getexternalstoragedirectory (). toString () + "/ Download_image.jpg ")," image/* "); mcontext.startactivity (intent);}});};}
Just write it in the activity:
Image = (ImageView) Findviewbyid (r.id.image); new Loadandsaveimage (Getapplicationcontext (), "http:// Toolongdidntread.com/wp-content/uploads/2011/06/upload.png ", image);
Three parameters are content, the URL of the picture, the imageview you want to load
BTW, because the stored jpg file name is fixed here, please use random number or name when loading multiple images.
SOURCE is here: →http://download.csdn.net/detail/edwardwayne/8621419
Android Load network pictures and keep the cache, click to open at any time