save images in cache android

12,331

Solution 1

This is a good example for this. Caching Bitmaps. They tell you how to do a memory cache as well as disk cache.

Solution 2

If you are looking for a quick and dirty solution you can store the mapping between file and url in shared preferences like this:

public class FileCache
{
    private File                cacheDir;
    private File                cacheDirEvent;
    private File                cacheDirPromotion;

    public FileCache(final Context context)
    {
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        {
            this.cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "TNLRadio/res/shows");
            this.cacheDirEvent = new File(android.os.Environment.getExternalStorageDirectory(), "TNLRadio/res/events");
            this.cacheDirPromotion = new File(android.os.Environment.getExternalStorageDirectory(),
                            "TNLRadio/res/promotion");
        }
        else
        {
            this.cacheDir = context.getCacheDir();
            this.cacheDirEvent = context.getCacheDir();
            this.cacheDirPromotion = context.getCacheDir();
        }
        if (!this.cacheDir.exists() && !this.cacheDirEvent.exists() && !this.cacheDirPromotion.exists())
        {
            this.cacheDir.mkdirs();
            this.cacheDirEvent.mkdir();
            this.cacheDirPromotion.mkdir();
        }
    }

    public File getFile(final Context context, final String url) throws FileNotFoundException
    {
        // retrieve filename/location from shared preferences based on the the url
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        String filename = settings.getString(url, null);

        if (url == null)
        {
            throw new FileNotFoundException();
        }

        final File f = new File(this.cacheDir, filename);
        return f;
    }

    public void downloadAndCache(final Context context, final String url)
    {
        // TODO: download the file and save to the filesystem
        // TODO: generate a the filename and push into saved preferences
        String filename = "";

        // save file into the share preferences so we can get it back late
        saveFileToMap(context, url, filename);
    }

    private void saveFileToMap(final Context context, final String url, final String filename)
    {
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

        // save the pair into shared preferences
        final Editor editor = settings.edit();
        editor.putString(url, filename);
        editor.commit();
    }
}
Share:
12,331
madu243
Author by

madu243

Updated on June 04, 2022

Comments

  • madu243
    madu243 almost 2 years

    My android project layout xml file has three button. which are shows, events & home. when buttons click they download images from url & save them in cache. So now downloaded are completed once it is clicked. when click the shows button again it must show the downloaded images taking from cache.

    The problem is I download all the images in one cache dir. when button click separately they load default images. because can't identify images. my code as following way.

    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(),"TNLRadio/res/shows");
                cacheDirEvent=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/events");
                cacheDirPromotion=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/promotion");
            }
            else{
                cacheDir=context.getCacheDir();
                cacheDirEvent=context.getCacheDir();
                cacheDirPromotion=context.getCacheDir();
            }
            if(!cacheDir.exists()&& !cacheDirEvent.exists() && !cacheDirPromotion.exists()){
                cacheDir.mkdirs();
                cacheDirEvent.mkdir();
                cacheDirPromotion.mkdir();
            }
        }
    
        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;
    
        } 
    

    pls help me. i'm stuck with this question

    After that I have done this. but same answer.......why is it?

    public FileCache(Context context,int i){
            //Find the dir to save cached images
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)&& i==3)
                cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/shows");
            else if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)&& i==2)
                cacheDirEvent=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/events");
            else if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) && i==1)
                cacheDirHome=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/home");
            else 
                cacheDir=context.getCacheDir();
                cacheDirEvent=context.getCacheDir();
                cacheDirHome=context.getCacheDir();
            if(!cacheDir.exists())
                cacheDir.mkdirs();
            else if (!cacheDirHome.exists())
                cacheDirHome.mkdirs();
            else if  (!cacheDirEvent.exists())
                cacheDirEvent.mkdirs();
    
        }
    
        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);
            if(url.substring(0, 26).equals("http://tnlradio/promotions")){
            File f = new File(cacheDirHome, filename);
            return f;}
            else if(url.equals("http://stream.tnlradio.com/images/dilshan-ishara.jpg")){
                File f = new File(cacheDirEvent, filename);
                return f;}
            else{
                File f = new File(cacheDir, filename);
                return f;
            }
    
        }
    

    pls help me