Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. package com.samir;
  2.  
  3. import java.io.File;
  4.  
  5. import android.content.Context;
  6.  
  7. public class FileCache {
  8.  
  9. private File cacheDir;
  10.  
  11. public FileCache(Context context){
  12. //Find the dir to save cached images
  13. if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
  14. cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
  15. else
  16. cacheDir=context.getCacheDir();
  17. if(!cacheDir.exists())
  18. cacheDir.mkdirs();
  19. }
  20.  
  21. public File getFile(String url){
  22. //I identify images by hashcode. Not a perfect solution, good for the demo.
  23. String filename=String.valueOf(url.hashCode());
  24. //Another possible solution (thanks to grantland)
  25. //String filename = URLEncoder.encode(url);
  26. File f = new File(cacheDir, filename);
  27. return f;
  28.  
  29. }
  30.  
  31. public void clear(){
  32. File[] files=cacheDir.listFiles();
  33. if(files==null)
  34. return;
  35. for(File f:files)
  36. f.delete();
  37. }
  38.  
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement