vendredi 30 octobre 2015

android delete old cache data programmatically

I'm using android disk cache to cache all images loaded from server.

I want to delete all those cache images that has not been used for atleast 2 weeks and keep the remaining.

Here's my code to add cache Images

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");
                }

            }
        }
    }
}

Any help is welcome... let me know if you need any more details...

Aucun commentaire:

Enregistrer un commentaire