title: android delete old cache data programmatically
body: <p>I'm using android disk cache to cache all images loaded from server.</p>
  
  <p>I want to delete all those cache images that has not been used for atleast 2 weeks and keep the remaining.</p>
  
  <p>Here's my code to add cache Images</p>
  
  <pre><code>public void addBitMaptoCache(String key,Bitmap bmp)
  {
      Log.e(TAG, "Cache Creation Running");
  
      if(null != defaultBitmaps){
          for(Bitmap dbmp:defaultBitmaps){
              if(dbmp.sameAs(bmp)){
                  return;
              }
          }
      }
  
      if(directory!=null)
      {
  
          if(!directory.exists())
          {
              directory.mkdirs();
          }
  
          if(directory.exists())
          {
              if(directory.isDirectory())
              {
  
                  File destination = new File(directory, key + ".jpg");
  
                  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                  bmp.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
  
                  FileOutputStream fo;
                  try {
                      destination.createNewFile();
                      fo = new FileOutputStream(destination);
                      fo.write(bytes.toByteArray());
                      fo.close();
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
                  destination = new File(directory, key + ".jpg");
  
                  if(destination.exists())
                  {
                      Log.d(TAG, "Cache Created");
                  }
                  else
                  {
                      Log.e(TAG, "Cache not Created");
                  }
  
              }
          }
      }
  }
  </code></pre>
  
  <p>Any help is welcome... let me know if you need any more details...</p>