-1

I'm making an android app and want to display to the user how much space I am taking up on their phone with data.

I currently am getting the size of the database file, and package application file, and adding them together but it is no where near the statistics shown in the android app settings.

I'm wondering if there is a way to get the data shown in the settings app: enter image description here

as my current approach:

long dbsize = DAL.Repository.getDBSizeinKB(); // is 18 MB
ApplicationInfo appinfo = a.PackageManager.GetApplicationInfo(a.ApplicationInfo.PackageName, 0);
long appsize = new FileInfo(appinfo.SourceDir).Length / 1000; // 4MB
string spaceUsed = "";
long totalsize = dbsize + appsize;
spaceUsed = totalsize.ToString() + " kB";
if (totalsize >= 1000)
    spaceUsed = (totalsize / 1000).ToString() + " MB";
sizeView.Text = "Space used: " + spaceUsed; // 22MB

is off.

panthor314
  • 318
  • 1
  • 14

1 Answers1

0

Try getting the root directory of your app and getting the size of all its directory tree recursively:

PackageManager m = getPackageManager();
String s = getPackageName();
long size;
try {
    PackageInfo p = m.getPackageInfo(s, 0);
    s = p.applicationInfo.dataDir;
    File directory = new File(s);
    size = folderSize(directory);
} catch (PackageManager.NameNotFoundException e) {
    Log.w("yourtag", "Error Package name not found ", e);
}

folderSize():

public static long folderSize(File directory) {
    long length = 0;
    for (File file : directory.listFiles()) {
        if (file.isFile())
            length += file.length();
        else
            length += folderSize(file);
    }
    return length;
}
Community
  • 1
  • 1