Ejemplo n.º 1
0
  public static Bitmap getCachedBitmap(Context context, String fileName, int cacheDurability) {
    if (isStorageAvailable()) {
      Bitmap bitmap = null;
      File file = new File(Tools.getExternalCacheDir(context), fileName);

      if (file.exists()) {
        if ((file.lastModified() + (cacheDurability * 3600 * 1000)) < System.currentTimeMillis())
          return null;

        try {

          FileInputStream input = new FileInputStream(file);
          bitmap = BitmapFactory.decodeStream(input);

          input.close();

        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

        return bitmap;
      } else {
        return null;
      }
    } else {
      return null;
    }
  }
Ejemplo n.º 2
0
  public static String getCachedString(Context context, String fileName, int cacheDurability) {
    if (isStorageAvailable()) {
      StringBuilder contents = new StringBuilder();
      File file = new File(Tools.getExternalCacheDir(context), fileName);

      if (file.exists()) {
        if ((file.lastModified() + (cacheDurability * 3600 * 1000)) < System.currentTimeMillis())
          return null;

        try {

          BufferedReader input = new BufferedReader(new FileReader(file));
          String line = null;

          while ((line = input.readLine()) != null) {
            contents.append(line);
            contents.append(System.getProperty("line.separator"));
          }

          input.close();

        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

        return contents.toString();
      } else {
        return null;
      }
    } else {
      return null;
    }
  }
Ejemplo n.º 3
0
  public static boolean remove(Context context, String fileName) {
    if (isStorageWritable()) {
      File dir = Tools.getExternalCacheDir(context);

      if (dir.isDirectory()) {
        File file = new File(Tools.getExternalCacheDir(context), fileName);

        if (file.exists()) file.delete();

        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }
Ejemplo n.º 4
0
  public static boolean removeAll(Context context) {
    if (isStorageWritable()) {
      File dir = Tools.getExternalCacheDir(context);

      if (dir.isDirectory()) {
        String[] files = dir.list();
        int i;
        for (i = 0; i < files.length; i++) {
          File file = new File(Tools.getExternalCacheDir(context), files[i]);

          if (file.exists()) file.delete();
        }

        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }
Ejemplo n.º 5
0
  public static boolean isCachedString(Context context, String fileName, int cacheDurability) {
    if (isStorageAvailable()) {
      File file = new File(Tools.getExternalCacheDir(context), fileName);

      if (file.exists()) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }
Ejemplo n.º 6
0
  public static void setCachedString(Context context, String fileName, String content) {
    if (isStorageWritable()) {
      File file = new File(Tools.getExternalCacheDir(context), fileName);

      try {

        file.createNewFile();
        BufferedWriter output = new BufferedWriter(new FileWriter(file));

        output.write(content);
        output.newLine();

        output.close();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 7
0
  public static void setCachedBitmap(Context context, String fileName, Bitmap bitmap) {
    if (isStorageWritable()) {
      File file = new File(Tools.getExternalCacheDir(context), fileName);

      if (file != null) {
        try {

          file.createNewFile();
          FileOutputStream output = new FileOutputStream(file);

          bitmap.compress(CompressFormat.JPEG, 100, output);
          output.flush();

          output.close();

        } catch (NullPointerException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }