清除 Android 图库缓存

Clear Android Gallery Cache

我使用这段代码来清除已安装应用程序的缓存数据,它在 root 设备上完美运行:

PackageManager  pm = getPackageManager();
    // Get all methods on the PackageManager
    Method[] methods = pm.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getName().equals("freeStorage")) {
            // Found the method I want to use
            try {
                long desiredFreeStorage = 8 * 1024; // Request for 8GB of free space
                m.invoke(pm, desiredFreeStorage , null);
                Toast.makeText(this, "Cache Cleared!", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                txtView.setText(e.getMessage());
            }
            break;
        }
    }

但是此代码仅适用于已安装的应用程序,不适用于系统安装的应用程序,例如(图库),我想以编程方式清除图库应用程序的缓存,例如此操作: Clear Gallery Cache

在非 root 设备上是不可能的,因为 Android 不允许它,因为 Marshmallow, 请仔细阅读此答案以获取更多详细信息,