How to get size of the app data in Android

Published by Igor Khrupin on

Here is few lines how to get size of directory in Android.
To get size of directory you need sum sizes of all files in this directory and subdirectories.

So, we need recursion.

long sizeOfCacheDir = getCacheDirSize(getCacheDir());
textViewSize.setText("Cache dir size = " + sizeOfCacheDir + " bytes");

Where :

private long getCacheDirSize(File file) {
    long size = 0;
    for (File f : file.listFiles()) {
        if (f.listFiles() == null) {
            size = size + f.length();
        } else {
            size = size + getCacheDirSize(f);
        }
    }
    return size;
}

Short sample project here on my github:

Download it from github

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.