码迷,mamicode.com
首页 > 移动开发 > 详细

android加载网络图片并保留缓存,随时点击打开

时间:2015-04-22 22:24:36      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:android开发   download   image   imageview   asynctask   

今天工作需要写了一个保留网络图片缓存的类,和大家分享一下


其实实现原理很简单,也就是从网上下载图片数据,一边将数据转成drawable并加载到指定的imageview

一边保存成download_image.jpg,在点击imageview时候用intent将图片打开


我将处理图片的过程写成了类

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 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);
				}
			});
		};
	}

}

只要在activity中这样写:

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

三个参数分别是content,图片的url,你要加载的imageview

BTW,由于这里设置的储存的jpg文件名字是固定的,加载多张图片时请用随机数或者自行命名


源码在这里:→http://download.csdn.net/detail/edwardwayne/8621419


android加载网络图片并保留缓存,随时点击打开

标签:android开发   download   image   imageview   asynctask   

原文地址:http://blog.csdn.net/edwardwayne/article/details/45198887

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!