注:此处只是获取自己的apk的大小。
首先我们需要的是权限:
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
接下来当然是获取PackageManager了啊:PackageManager mPackageManager=getPackageManager()
try {
PackageInfo pInfo=mPackageManager.getPackageInfo(getPackageName(),0)
mPackageName=pInfo.packageName
} catch (NameNotFoundException e) {
e.printStackTrace()
}
在接下来
//返回一个安装到设备上的所有应用程序包列表
List<ApplicationInfo>installList=mPackageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES)
for (int i = 0i <installList.size()i++) {
ApplicationInfo info=installList.get(i)
if(info.packageName.equals(mPackageName)){//如果这个packagename和当前apk的packagename一样,你就可以获取它的大小了。
String dir=info.publicSourceDir
int size=Integer.valueOf((int)new File(dir).length())
BigDecimal apkSize=parseApkSize(size)
tvSize.setText("apk大小:"+apkSize+"M")//最后显示的应用程序大小了
}
}
java中保留两位小数:private BigDecimal parseApkSize(int size) {
BigDecimal bd = new BigDecimal((double)size/(1024*1024))
BigDecimal setScale = bd.setScale(2, BigDecimal.ROUND_DOWN)
return setScale
File root = Environment.getRootDirectory()StatFs sf = new StatFs(root.getPath())
long blockSize = sf.getBlockSize()
long blockCount = sf.getBlockCount()
long availCount = sf.getAvailableBlocks()
Log.d("TAG", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:"+ blockSize * blockCount / (1024 * 1024) + "MB")
Log.d("TAG","可用的block数目::" + availCount + ",可用大小:" + availCount * blockSize / (1024 * 1024)+ "MB")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)