将图像保存到缓存android

时间:2013-07-19 07:18:47

标签: android performance

我创建了一个应用。单击三个按钮时,将图像显示为轮播。哪个是事件,节目和&家。这些图像是从互联网上下载的。这些图像保存在缓存中。为此,我使用了三个缓存目录。但它不起作用。另外,我想从目录中单独访问这些图像。

这是我的代码

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

        public FileCache(Context context,int i)
        {

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

            public File getFile( 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;
                if(url.substring(0, 26).equals("http://tnlradio/promotions")){
                    File f = new File(cacheDirPromotion, filename);
                    return f;
                    }
                    else if(url.substring(0, 26).equals("http://tnlradio/promotions")){
                        File f = new File(cacheDirEvent, filename);
                        return f;
                        }
                    else{
                        File f = new File(cacheDir, filename);
                        return f;
                    }
            }

这里我曾经使用整数值选择目录。这意味着当单击这些按钮时,整数将传递给FileCash。那么如何分别加载精确的图像。现在没有加载任何图像。

请帮助我。谢谢

0 个答案:

没有答案