代碼實現(xiàn)(封裝后)
/**
* 緩存到SDCard
* 封裝兩個方法,
* 1:put
* 2:get
*/
public class LocalCache {
public void putImage(String imageUrl, Bitmap bitmap) throws Exception {
String newImage = MD5Unit.encode(imageUrl);
Log.e("newImage", newImage);
File file = new File(Environment.getExternalStorageDirectory() + "/BCache/" + newImage);
Log.e("看看目錄結(jié)構(gòu)", file.getAbsolutePath());
File parentFile = file.getParentFile();
Log.e("看看文件的父目錄", parentFile.getAbsolutePath());
if (!parentFile.exists()) {
parentFile.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
//Bitmap可以直接跟IO流就行直接交互
//compress 有三個參數(shù)嫁赏,參數(shù)1:圖片格式 參數(shù)2:對原圖片進行壓縮熄守,參數(shù)3:流
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Log.e("-----", "圖片寫入成功 66噠");
fos.flush();
fos.close();
}
public Bitmap getImage(String imageUrl) throws Exception {
String newImage = MD5Unit.encode(imageUrl);
File file = new File(Environment.getExternalStorageDirectory() + "/BCache/" + newImage);
InputStream in = new FileInputStream(file);
Log.e("獲取圖片", "成功么么噠");
return BitmapFactory.decodeStream(in);
}
}