Android keeps cache on network pictures loaded at any time

Source: Internet
Author: User
Tags stub

The principle is to download the image data from the Internet, while turning the data into drawable and loading to the specified ImageView

Save it as a download_image.jpg and open the picture with intent when clicked ImageView


I write the process of processing a picture as a class

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, String str, ImageView view) {
TODO auto-generated Constructor stub
This.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> {

@Override
Protected Void doinbackground (String ... surl) {
TODO auto-generated Method Stub
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL (surl[0]);
Connection = (httpurlconnection) url.openconnection ();
Connection.connect ();

Download the file
input = 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");

byte 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 (IOException 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 () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent 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 this 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, you want to load the ImageView

BTW, because the stored jpg file name is fixed here, please use random number or name when loading multiple pictures.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.