2

我的 android 项目布局 xml 文件有三个按钮。它们是表演、活动和家庭。当按钮单击时,它们会从 url 下载图像并将它们保存在缓存中。所以现在下载完成一次点击。当再次单击显示按钮时,它必须显示从缓存中下载的图像。

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

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

    }

请帮助我

4

2 回答 2

2

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

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();
    }
}
于 2013-07-19T01:08:02.873 回答
2

这是一个很好的例子。缓存位图。他们告诉您如何进行内存缓存和磁盘缓存。

于 2013-07-19T01:05:34.843 回答