在缓存android中保存图像

时间:2013-07-19 00:32:38

标签: android performance

我的android项目布局xml文件有三个按钮。这是表演,活动和家。当按钮点击时,他们从url& amp;将它们保存在缓存中。所以现在下载完成后点击完成。当再次单击“显示”按钮时,它必须显示从缓存中获取的下载图像。

问题是我在一个缓存目录中下载了所有图像。当按钮单独单击时,它们会加载默认图像因为无法识别图像。我的代码如下。

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;

    } 
请帮助我。我坚持这个问题

之后我就这样做了。但同样的答案.......为什么会这样?

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

    }

请帮帮我

2 个答案:

答案 0 :(得分:2)

如果您正在寻找一个快速而肮脏的解决方案,您可以将文件和网址之间的映射存储在共享首选项中,如下所示:

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

答案 1 :(得分:1)

这是一个很好的例子。 Caching Bitmaps.他们告诉你如何进行内存缓存以及磁盘缓存。