package com.daifan.service; import android.content.Context; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; public class FileCache { private File cacheDir; private File imgDir; public FileCache(Context context) { //Find the dir to save cached images if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "51daifan"); else cacheDir = context.getCacheDir(); if (!cacheDir.exists()) cacheDir.mkdirs(); imgDir = new File(cacheDir, "images"); if (!imgDir.exists()) imgDir.mkdirs(); } public File getCacheDir(){ return this.cacheDir; } public File getFile(String url) { //I identify images by hashcode. Not a perfect solution, good for the demo. String filename = String.valueOf(url.hashCode()); //Another possible solution (thanks to grantland) //String filename = URLEncoder.encode(url); File f = new File(cacheDir, filename); return f; } public File createTmpImg() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "jpg-" + timeStamp + "_"; File image = File.createTempFile( imageFileName, ".jpg", this.imgDir ); return image; } public void clear() { File[] files = cacheDir.listFiles(); if (files == null) return; for (File f : files) f.delete(); } }