【问题标题】:save images in cache android将图像保存在缓存android中
【发布时间】:2013-07-19 10:16:40
【问题描述】:

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

    }

请帮帮我

【问题讨论】:

    标签: android performance


    【解决方案1】:

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

    【讨论】:

    • 对于我可能需要保存的大量照片,您有什么建议?我希望使用尽可能少的空间,但在阅读时具有一些不错的性能。扭动速度也很重要。
    • @MihaiBratulescu 这可能取决于图像的大小。如果这些将是 JPEG,我会说磁盘缓存可以有很多,甚至 100?至于内存缓存,它们占用了相当多的内存空间。您可能只想对当前显示的内容进行内存缓存,并且可能更远一些,具体取决于图像大小。
    【解决方案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();
        }
    }
    

    【讨论】:

    • 离线时图片是否可用,并保存网址?
    • 是的,使用缓存,您应该始终在尝试下载之前检查缓存中的文件。因此它在缓存中,不需要互联网连接
    猜你喜欢
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2013-06-03
    • 2010-12-29
    • 2014-08-16
    • 2014-10-14
    • 2012-01-16
    相关资源
    最近更新 更多