import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.WeakHashMap;
- import org.apache.http.HttpStatus;
- import com.android.lalala.util.lalalaApplication;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- import android.os.Handler;
- import android.widget.ImageView;
- /**
- * 图片下载与缓存 思路是,先查看内存,后检查SDcard,没有的话联网进行下载。
- */
- public class ImageLoader {
- private ImageView imageView = null;
- private String urlPath = "";
- private WeakHashMap<String, Bitmap> picsHashMap = null;
- private String urlHashCode = "";
- private String filePath = "";
- private Handler handler = null;
- private Bitmap handlerBitmap = null;
- /**
- * 构造器
- *
- * @param imageView
- * imageview对象
- * @param urlPath
- * 下载的url地址
- * @param filePath
- * 缓存文件夹名称
- */
- public ImageLoader(ImageView imageView, String urlPath, String filePath) {
- super();
- this.imageView = imageView;
- this.urlPath = urlPath;
- this.filePath = filePath;
- urlHashCode = String.valueOf(urlPath.hashCode());
- // 从application中获取picHashMap对象
- picsHashMap = lalalaApplication.getInstance().getPicHashMap();
- handler = new Handler();
- new imageLoaderThread().start();
- }
- /**
- * 图片下载线程
- */
- private class imageLoaderThread extends Thread {
- @Override
- public void run() {
- super.run();
- if (readFromRAM()) {
- return;
- }
- if (readFromSDcard()) {
- return;
- }
- httpDownload();
- }
- }
- /**
- * 开始下载
- */
- private void httpDownload() {
- try {
- URL url = new URL(urlPath);
- HttpURLConnection connection = (HttpURLConnection) url
- .openConnection();
- connection.setConnectTimeout(10 * 1000);
- if (connection.getResponseCode() == HttpStatus.SC_OK) {
- InputStream is = connection.getInputStream();
- Bitmap bitmap = BitmapFactory.decodeStream(is);
- setBitmap(bitmap);
- lalalaApplication.getInstance().getPicHashMap()
- .put(urlHashCode, bitmap);
- saveToSDcard(bitmap);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 将bitmap保存至SD卡上
- *
- * @param bitmap
- * bitmap
- */
- private void saveToSDcard(Bitmap bitmap) {
- try {
- String fileName = filePath + "/" + urlHashCode + ".JPG";
- File file = new File(filePath);
- if (!file.exists()) {
- file.mkdir();
- }
- BufferedOutputStream outputStream = new BufferedOutputStream(
- new FileOutputStream(new File(fileName)));
- bitmap.compress(CompressFormat.JPEG, 100, outputStream);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * 从内存中读取bitmap图片数据
- *
- * @return true内存中有数据 false 内存中无数据
- */
- private boolean readFromRAM() {
- if (picsHashMap.containsKey(urlHashCode)) {
- Bitmap bitmap = picsHashMap.get(urlHashCode);
- setBitmap(bitmap);
- return true;
- }
- return false;
- }
- /**
- * 从SD卡读取图片
- *
- * @return trueSDcard中有数据 false SDcard中无数据
- */
- private boolean readFromSDcard() {
- String fileName = filePath + "/" + urlHashCode + ".JPG";
- File file = new File(fileName);
- if (!file.exists()) {
- return false;
- } else {
- Bitmap bitmap = BitmapFactory.decodeFile(fileName);
- picsHashMap.put(urlHashCode, bitmap);
- setBitmap(bitmap);
- return true;
- }
- }
- /**
- * 设置图片
- *
- * @param bitmap
- * 图片
- */
- private void setBitmap(Bitmap bitmap) {
- this.handlerBitmap = bitmap;
- handler.post(new Runnable() {
- @Override
- public void run() {
- imageView.setImageBitmap(handlerBitmap);
- }
- });
- }
- }