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