当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How can I get informations within code about the cache of my app?

问题描述:

I want to check where my apps cache is raising all the time.

So I need a method to access informations about the current cache at any time.

I think there must be such a method, because I could see the size of the cache in the App-Info of my android-smartphone.

The app is running in the background and I want to log when the size of the cache is raising.

网友答案:

Use below code snippets for your solution.

public static byte[] retrieveData(Context context, String name) throws IOException {

        File cacheDir = context.getCacheDir();
        File file = new File(cacheDir, name);

        if (!file.exists()) {
            // Data doesn't exist
            return null;
        }

        byte[] data = new byte[(int) file.length()];
        FileInputStream is = new FileInputStream(file);
        try {
            is.read(data);
        }
        finally {
            is.close();
        }

        return data;
    }

Here is complete CacheManager class.

When to clear the cache dir in Android?

分享给朋友:
您可能感兴趣的文章:
随机阅读: