自己寫的,發上來大家看看,基礎不是太好。
package com.android.lalala.net;
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);
}
});
}
}