【问题标题】:How to cache a cloud image in either internal or external memory of an android device如何在 android 设备的内部或外部存储器中缓存云图像
【发布时间】:2016-04-25 15:09:25
【问题描述】:

您好 Android 成员,当存在外部存储器时,我的 Android 代码在 Android 设备上运行良好。实际上,来自 URL 的图像缓存在外部设备中并在我的应用程序上很好地显示,但我的问题是:任何人都可以指导我如何将图像保存在内部存储器中,以及在未安装外部存储器的情况下在设备上?图像仅显示在具有存储卡的设备上。

public class FileCache {

private File cacheDir;

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(),"TempImages");
    else
        cacheDir=context.getCacheDir();
    if(!cacheDir.exists())
        cacheDir.mkdirs();
}

public File getFile(String url){
    String filename=String.valueOf(url.hashCode());
    File f = new File(cacheDir, filename);
    return f;

}

public void clear(){
    File[] files=cacheDir.listFiles();
    if(files==null)
        return;
    for(File f:files)
        f.delete();
}

}

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用context.getFilesDir(),而不是使用context.getCacheDir()。原因是getCacheDir()是用来存放轻量级缓存文件的。

    但是,我强烈建议您使用成熟的库,例如 Picasso 或 Glide 来加载和缓存图像。

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 2012-06-27
      • 1970-01-01
      • 2012-07-18
      • 2013-01-16
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多