一个喜欢安静的博主

Bitmap 两种缓存方式

 一种方式是把Bitmap 缓存到内存,但是我们需要使用弱引用来缓存这个Bitmap: Memory Cache

另一种方式是我们的内存即使弱引用了这个还是不够用,或者频繁访问网络造成流量消耗太多我们可以把这个bitmap先保存在本地sd卡中,避免过多的流量消耗。File Cache

1.   Memory Cache

  1. publicclassMemoryCache{

  2. privatestaticfinalString TAG ="MemoryCache";

  3. //WeakReference Map: key=string, value=Bitmap

  4. privateWeakHashMap<String,Bitmap> cache =newWeakHashMap<String,Bitmap>();

  5. /**

  6.      * Search the memory cache by a unique key. 

  7.      * @param key Should be unique. 

  8.      * @return The Bitmap object in memory cache corresponding to specific key.

  9.      * */

  10. publicBitmap get(String key){

  11. if(key !=null)

  12. return cache.get(key);

  13. returnnull;

  14. }

  15. /**

  16.      * Put a bitmap into cache with a unique key.

  17.      * @param key Should be unique.

  18.      * @param value A bitmap.

  19.      * */

  20. publicvoid put(String key,Bitmap value){

  21. if(key !=null&&!"".equals(key)&& value !=null){

  22.             cache.put(key, value);

  23. //Log.i(TAG, "cache bitmap: " + key);

  24. Log.d(TAG,"size of memory cache: "+ cache.size());

  25. }

  26. }

  27. /**

  28.      * clear the memory cache.

  29.      * */

  30. publicvoid clear(){

  31.         cache.clear();

  32. }

  33. }

2.    File Cache

  1. publicclassFileCache{

  2. privatestaticfinalString TAG ="MemoryCache";

  3. privateFile cacheDir;//the directory to save images

  4. /**

  5.      * Constructor

  6.      * @param context The context related to this cache.

  7.      * */

  8. publicFileCache(Context context){

  9. // Find the directory to save cached images

  10. if(android.os.Environment.getExternalStorageState()

  11. .equals(android.os.Environment.MEDIA_MOUNTED))

  12.             cacheDir =newFile(

  13.                     android.os.Environment.getExternalStorageDirectory(),

  14. Config.CACHE_DIR);

  15. else

  16.             cacheDir = context.getCacheDir();

  17. if(!cacheDir.exists())

  18.             cacheDir.mkdirs();

  19. Log.d(TAG,"cache dir: "+ cacheDir.getAbsolutePath());

  20. }

  21. /**

  22.      * Search the specific image file with a unique key.

  23.      * @param key Should be unique.

  24.      * @return Returns the image file corresponding to the key.

  25.      * */

  26. publicFile getFile(String key){

  27. File f =newFile(cacheDir, key);

  28. if(f.exists()){

  29. Log.i(TAG,"the file you wanted exists "+ f.getAbsolutePath());

  30. return f;

  31. }else{

  32. Log.w(TAG,"the file you wanted does not exists: "+ f.getAbsolutePath());

  33. }

  34. returnnull;

  35. }

  36. /**

  37.      * Put a bitmap into cache with a unique key.

  38.      * @param key Should be unique.

  39.      * @param value A bitmap.

  40.      * */

  41. publicvoid put(String key,Bitmap value){

  42. File f =newFile(cacheDir, key);

  43. if(!f.exists())

  44. try{

  45.                 f.createNewFile();

  46. }catch(IOException e){

  47.                 e.printStackTrace();

  48. }

  49. //Use the util's function to save the bitmap.

  50. if(BitmapHelper.saveBitmap(f, value))

  51. Log.d(TAG,"Save file to sdcard successfully!");

  52. else

  53. Log.w(TAG,"Save file to sdcard failed!");

  54. }

  55. /**

  56.      * Clear the cache directory on sdcard.

  57.      * */

  58. publicvoid clear(){

  59. File[] files = cacheDir.listFiles();

  60. for(File f : files)

  61.             f.delete();

  62. }

  63. }

3.    关于上面的如何把BitMap 保存到文件中,这里有个工具方法:BitmapHelper 这个类中我们写保存bitmap的代码

  1. publicstaticboolean saveBitmap(File file,Bitmap bitmap){

  2. if(file ==null|| bitmap ==null)

  3. returnfalse;

  4. try{

  5. BufferedOutputStream out =newBufferedOutputStream(newFileOutputStream(file));

  6. return bitmap.compress(CompressFormat.JPEG,100, out);

  7. }catch(FileNotFoundException e){

  8.         e.printStackTrace();

  9. returnfalse;

  10. }

  11. }



评论

© x-surfer | Powered by LOFTER