-1

我正在制作一个android应用程序,并希望向用户显示我使用数据在手机上占用了多少空间。如何获取当前android应用程序占用的数据量?

我目前正在获取数据库文件的大小,并打包应用程序文件,并将它们添加到一起,但它没有位于android应用程序设置中显示的统计数据附近。

我不知道是否有一种方式来获得在设置应用程序中显示的数据: enter image description here

因为我目前的做法:

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 

关闭。

回答

0

尝试getting the root directory of your appgetting 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; 
} 
相关问题