LruCache圖片內存緩存(三級緩存——SDCard內存)

代碼實現(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("看看目錄結構", 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);

}

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容