View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.utils.image;
20  
21  import java.io.File;
22  
23  import android.content.Context;
24  
25  /**
26   * Created by The eXo Platform SAS Author : eXoPlatform exo@exoplatform.com Apr
27   * 5, 2012
28   */
29  
30  public class FileCache {
31  
32    private File cacheDir;
33  
34    public FileCache(Context context, String folder) {
35      // Find the dir to save cached images
36      if (android.os.Environment.getExternalStorageState()
37                                .equals(android.os.Environment.MEDIA_MOUNTED))
38        cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "eXo/" + folder);
39      else
40        cacheDir = context.getCacheDir();
41      if (!cacheDir.exists())
42        cacheDir.mkdirs();
43    }
44  
45    public String getCachePath() {
46      return cacheDir.getAbsolutePath();
47    }
48  
49    public File getFile(String url) {
50      String filename = String.valueOf(url.hashCode());
51      File f = new File(cacheDir, filename);
52      return f;
53  
54    }
55  
56    public File getFileFromName(String filename) {
57      File f = new File(cacheDir, filename);
58      return f;
59    }
60  
61    public void clear() {
62      File[] files = cacheDir.listFiles();
63      if (files == null)
64        return;
65      for (File f : files)
66        f.delete();
67    }
68  
69  }