在android缓存中保存图像
我的android项目布局xml文件有三个按钮。节目、活动和家庭。当按钮单击时,它们从url下载图像并将其保存在缓存中。所以现在下载完成后,点击它。再次单击“显示”按钮时,它必须显示从缓存中下载的图像 问题是我将所有图像下载到一个缓存目录中。当按钮单独单击时,它们将加载默认图像。因为无法识别图像。我的代码如下所示在android缓存中保存图像,android,performance,Android,Performance,我的android项目布局xml文件有三个按钮。节目、活动和家庭。当按钮单击时,它们从url下载图像并将其保存在缓存中。所以现在下载完成后,点击它。再次单击“显示”按钮时,它必须显示从缓存中下载的图像 问题是我将所有图像下载到一个缓存目录中。当按钮单独单击时,它们将加载默认图像。因为无法识别图像。我的代码如下所示 public FileCache(Context context){ //Find the dir to save cached images if (
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;
}
}
请帮助我这是一个很好的例子。它们告诉您如何进行内存缓存和磁盘缓存。这是一个很好的例子。它们告诉您如何进行内存缓存和磁盘缓存。如果您正在寻找一种快速而肮脏的解决方案,您可以将文件和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();
}
}
如果您正在寻找一个快速而肮脏的解决方案,您可以将文件和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();
}
}
当您脱机并保存url时,图像是否可用?是的,使用缓存时,您应该在尝试下载之前始终检查缓存中的文件。因此,它位于缓存中,不需要internet连接。当您脱机时,保存url后,图像是否可用?是的,使用缓存时,在尝试下载之前,您应该始终检查缓存中的文件。因此,它在缓存中,不需要internet连接。对于我需要保存的大量照片,您有何建议?我希望尽可能少的空间使用,但与一些体面的表现在阅读它。“扭动速度也很重要。”MihaiBratulescu说,这可能取决于图像的大小。如果这些是JPEG,我会说一个磁盘缓存可以有很多,甚至100个?至于内存缓存,它们会占用相当多的内存空间。您可能只想将当前显示的内容缓存在内存中,可能需要更远一点,具体取决于图像大小。对于我需要保存的大量照片,您有何建议?我希望尽可能少的空间使用,但与一些体面的表现在阅读它。“扭动速度也很重要。”MihaiBratulescu说,这可能取决于图像的大小。如果这些是JPEG,我会说一个磁盘缓存可以有很多,甚至100个?至于内存缓存,它们会占用相当多的内存空间。您可能只想将当前显示的内容缓存在内存中,根据图像大小,可能需要再缓存一点。