2

我创建了一个应用程序。单击三个按钮时将图像显示为轮播。分别是活动、表演和家庭。这些图像是从互联网上下载的。这些图像保存在缓存中。为此,我使用了三个缓存目录。但它不起作用。我也想从目录中单独访问这些图像。

这是我的代码

 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。那么如何单独加载精确的图像。现在没有加载任何图像。

请帮助我。谢谢

4

0 回答 0