三级缓存 一眼明了

package com.example.sanjidemo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.concurrent.Executors;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class MainActivity extends Activity {

	private LruCache neicun;
	private String sdPath;
	private ArrayList al;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 创建数据
		al = new ArrayList();

		al.add("http://f.hiphotos.baidu.com/image/h%3D360/sign=24ea84ec6a061d956246313e4bf60a5d/5ab5c9ea15ce36d358d27ee43ef33a87e850b114.jpg");
		al.add("http://h.hiphotos.baidu.com/image/h%3D360/sign=708907247d310a55db24d8f287474387/09fa513d269759eebad2fd1cb6fb43166c22df63.jpg");
		al.add("http://b.hiphotos.baidu.com/image/h%3D360/sign=a8163b3632d3d539de3d09c50a85e927/ae51f3deb48f8c54f512dee73e292df5e1fe7f30.jpg");
		al.add("http://h.hiphotos.baidu.com/image/h%3D360/sign=b9ed5b980833874483c5297a610dd937/55e736d12f2eb938d4997f3ad1628535e4dd6f05.jpg");
		al.add("http://c.hiphotos.baidu.com/image/h%3D360/sign=48f4fe092a738bd4db21b437918a876c/f7246b600c33874479b8aea8550fd9f9d62aa0e1.jpg");
		al.add("http://a.hiphotos.baidu.com/image/h%3D360/sign=b6f1a419ca1b9d1695c79c67c3dfb4eb/bba1cd11728b47107925cb0ac7cec3fdfd0323e9.jpg");
		al.add("http://g.hiphotos.baidu.com/image/h%3D360/sign=1c56df968fd4b31cef3c92bdb7d4276f/6a600c338744ebf84cf7a0eaddf9d72a6159a765.jpg");
		al.add("http://e.hiphotos.baidu.com/image/h%3D360/sign=b2b65dbed4160924c325a41de406359b/a08b87d6277f9e2f034327e01b30e924b999f3e7.jpg");
		al.add("http://b.hiphotos.baidu.com/image/h%3D360/sign=7692e44bdd33c895b97e9e7de1117397/1c950a7b02087bf47367c435f6d3572c10dfcf67.jpg");
		al.add("http://a.hiphotos.baidu.com/image/h%3D360/sign=7b39058ece80653864eaa215a7dca115/8cb1cb1349540923357ee3be9658d109b2de49df.jpg");
		al.add("http://g.hiphotos.baidu.com/image/h%3D360/sign=293df000adec8a130b1a51e6c7029157/c75c10385343fbf2cc0072ccb47eca8065388f35.jpg");
		al.add("http://c.hiphotos.baidu.com/image/h%3D360/sign=b9242b90550fd9f9bf17536f152fd42b/9a504fc2d5628535b701d6cd94ef76c6a6ef637c.jpg");
		al.add("http://c.hiphotos.baidu.com/image/h%3D360/sign=e2182d0c553d269731d30e5b65f9b24f/64380cd7912397dde3b088c25d82b2b7d1a2872e.jpg");
		al.add("http://b.hiphotos.baidu.com/image/h%3D360/sign=31531057adc379316268802fdbc5b784/a1ec08fa513d2697104dda0c57fbb2fb4216d8c1.jpg");
		al.add("http://a.hiphotos.baidu.com/image/h%3D360/sign=013282cdc1cec3fd943ea173e688d4b6/1f178a82b9014a904e2339bcab773912b31bee96.jpg");
		al.add("http://a.hiphotos.baidu.com/image/h%3D360/sign=2f1ade4b8626cffc762ab9b489004a7d/b3fb43166d224f4ab8fbdbc10df790529922d186.jpg");
		// 找控件
		ListView lv = (ListView) findViewById(R.id.lv);
		// 创建缓存
		neicun = new LruCache(1024 * 1024 * 50);
		// 获取SD内存
		sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
		// 设置数据库
		lv.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// 优化
				if (convertView == null) {
					convertView = new ImageView(MainActivity.this);
				}
				ImageView imageView = ((ImageView) convertView);
				imageView.setImageResource(R.drawable.hmn22);
				imageView.setTag(al.get(position));
				try {

					sanjihuancun(al.get(position), imageView);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return convertView;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public Object getItem(int position) {
				return al.get(position);
			}

			@Override
			public int getCount() {
				return al.size();
			}
		});

	}

	// 三级缓存 方法
	public Bitmap sanjihuancun(String imagePath, ImageView iv) throws Exception {
		// if(内存有图这张图片) { return 内存的图片}
		Bitmap bitmap = neicun.get(imagePath);
		System.out.println(neicun.size());
		if (bitmap != null) {
			System.out.println("在内存拿的图片");
			iv.setImageBitmap(bitmap);
			return bitmap;
		}
		// if(SD有图这张图片) { return SD的图片} 存内存
		File file = new File(sdPath + "/"
				+ URLEncoder.encode(imagePath, "utf-8"));
		if (file.exists()) {
			bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
			System.out.println("在SD拿的图片");
			neicun.put(imagePath, bitmap);
			iv.setImageBitmap(bitmap);
			return bitmap;
		}
		// 请求网络去拿图片 存取 sd卡 和 存一份
		gotoNet(imagePath, iv);
		return null;
	}

	// 点击清除
	public void click01(View v) {
		try {
			for (String path : al) {
				File file = new File(sdPath + "/"
						+ URLEncoder.encode(path, "utf-8"));
				if (file.delete()) {
					System.out.println("成功删除");
				}

			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 访问网络加载图片
	private void gotoNet(final String imagePath, final ImageView iv)
			throws Exception {

		new AsyncTask() {

			@Override
			protected Bitmap doInBackground(Void... params) {
				// 请求网络
				try {
					HttpURLConnection conn = (HttpURLConnection) new URL(
							imagePath).openConnection();
					conn.setConnectTimeout(5000);
					if (200 == conn.getResponseCode()) {
						InputStream in = conn.getInputStream();
						return BitmapFactory.decodeStream(in);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			protected void onPostExecute(final Bitmap bitmap) {

				// 给内存一份 往集合方一个
				neicun.put(imagePath, bitmap);

				// 给SD卡一份
				try {
					OutputStream stream = new FileOutputStream(new File(sdPath
							+ "/" + URLEncoder.encode(imagePath, "utf-8")));
					bitmap.compress(CompressFormat.JPEG, 100, stream);
				} catch (Exception e) {
					e.printStackTrace();
				}
				// 发消息展示

				runOnUiThread(new Runnable() {
					public void run() {
						System.out.println("在网络拿的图片");

						String tagPath = (String) iv.getTag();
						if (tagPath.equals(imagePath)) {
							iv.setImageBitmap(bitmap);
						}
					}
				});

				/*
				 * Message message = Message.obtain(handler, 1, bitmap);
				 * handler.sendMessage(message);
				 */
			};
		}.execute();
	}

}

你可能感兴趣的:(三级缓存 一眼明了)