【问题标题】:How to clear cache Android如何清除缓存Android
【发布时间】:2011-10-17 09:44:27
【问题描述】:

我需要找到一种方法来清除我的应用程序存储在缓存中的数据。基本上我正在使用 Fedor 的 (Lazy load of images in ListView) 惰性列表实现,我想在加载了例如 100 个图像时自动清除缓存.任何想法如何做到这一点?

编辑: 代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(this, mStrings);
    list.setAdapter(adapter);

    deleteCache(this);
    adapter.notifyDataSetChanged();


}

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}

【问题讨论】:

  • 你好,你有答案吗?我和你有同样的问题。我想要清除缓存以加载新图像。谢谢

标签: android list caching lazy-evaluation


【解决方案1】:

这将删除缓存

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}

【讨论】:

  • 通过deleteCache(this)调用该方法;
  • 如何知道缓存是否被删除?
  • 因为我认为如果缓存被删除,图像将重新开始加载,但它们不会。这就是我想要实现的目标。
  • 删除缓存后,尝试调用reload images。如果缓存已删除,请在文件 explorere->data->data->package name 中查看,缓存目录将为空。如果您在手机设备上运行,请查看您的管理应用程序,缓存将为 0.00
  • 请注意,如果您使用外部缓存,则必须将 context.getCacheDir() 替换为 context.getExternalCacheDir()
【解决方案2】:

希望对你有所帮助

public static void trimCache(Context context) {
    File dir = context.getCacheDir();
    if(dir!= null && dir.isDirectory()){
        File[] children = dir.listFiles();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            File temp;
            for (int i = 0; i < children.length; i++) {
                temp = children[i];
                temp.delete();
            }
        }

    }

} 

【讨论】:

  • 请不要在您的帖子中添加标签行或签名。你的名字和你的个人资料链接会自动显示在你写的每个问题或答案的底部。
【解决方案3】:

您可以调用系统服务clearApplicationUserData()(仅适用于>= KitKat版本)

所以你可以检查版本,然后一切都会好起来的 :) 这是代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ((ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE))
                    .clearApplicationUserData();
    }

【讨论】:

  • 欢迎来到 StackOverflow!除了提供使用 KitKat 或更高版本时的操作信息外,还请提供使用较低版本的用户的信息。
  • 实际上这部分代码完全崩溃了我的应用程序。
  • 此方法用于清除应用的所有数据,而不是应用的缓存!根据这个answer,预计应用程序会关闭,因为这个方法完全重置应用程序,所以应用程序不需要运行...
【解决方案4】:

如果使用 Apache Commons IO,您可以通过一行来完成此操作(不包括 try-catch):

try {
    FileUtils.deleteDirectory(context.getCacheDir());
} catch (IOException e) {
    // Do some error handling
}

【讨论】:

    【解决方案5】:

    使用 Kotlin,您只需调用

    File(context.cacheDir.path).deleteRecursively()
    

    结果与其他答案相同,但无需手动检查和循环

    【讨论】:

      【解决方案6】:

      丢弃旧适配器并将列表视图附加到适配器的新实例(但列表视图将丢失滚动位置)。示例:

          @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          m_adapter = new MyAdapter(this);
              final ListView listView = (ListView) getView()
                      .findViewById(R.id.my_list);
              listView.setAdapter(m_adapter);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        相关资源
        最近更新 更多