Android loads network images and retains the cache. Click to open them at any time.

Source: Internet
Author: User

Android loads network images and retains the cache. Click to open them at any time.

Today, I need to write a class that retains the network image cache. I 'd like to share it with you.

 

In fact, the implementation principle is very simple, that is, to download image data from the Internet, while converting the data into drawable and loading it to the specified imageview

Save the image as download_image.jpg and open the image with intent when you click imageview.

 

I wrote the image processing process 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 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
 
   {@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);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() {@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);}});};}}
 

Write in the activity as follows:

 

 

image = (ImageView) findViewById(R.id.image);new LoadAndSaveImage(getApplicationContext(), http://toolongdidntread.com/wp-content/uploads/2011/06/upload.png, image);

The three parameters are content, the image url, and the imageview to be loaded.

 

BTW, because the saved jpg file name is fixed here, please use a random number or name it yourself when loading multiple images

 

 

 

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.