FileCache.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package com.easemob.easeui.oss;
  2. import android.content.Context;
  3. import java.io.File;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLEncoder;
  6. public class FileCache {
  7. //�����ļ�Ŀ¼
  8. private File mCacheDir;
  9. /**
  10. * ���������ļ�Ŀ¼�������SD������ʹ��SD�����û����ʹ��ϵͳ�Դ��Ŀ¼
  11. * @param context
  12. * @param cacheDir ͼƬ�����һ��Ŀ¼
  13. */
  14. public FileCache(Context context, File cacheDir, String dir){
  15. if(android.os.Environment.getExternalStorageState()
  16. .equals(android.os.Environment.MEDIA_MOUNTED))
  17. mCacheDir = new File(cacheDir, dir);
  18. else
  19. mCacheDir = context.getCacheDir();// ��λ�ȡϵͳ���õĻ���洢·��
  20. if(!mCacheDir.exists()) mCacheDir.mkdirs();
  21. }
  22. public File getFile(String url){
  23. File f=null;
  24. try {
  25. //��url���б༭���������·������
  26. String filename = URLEncoder.encode(url,"utf-8");
  27. f = new File(mCacheDir, filename);
  28. } catch (UnsupportedEncodingException e) {
  29. e.printStackTrace();
  30. }
  31. return f;
  32. }
  33. public void clear(){//�����ļ�
  34. File[] files = mCacheDir.listFiles();
  35. for(File f:files)f.delete();
  36. }
  37. }