How does android get its own apk size ?, Get apk for android
I haven't updated my blog for a long time. Recently, I saw that the size of the app in the company's projects was reversed from the server, and it was not available yet. So xxx does not speak. The following shows how to obtain the size of your app.
Note: Only the apk size is obtained here.
First, we need permissions:
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
The next step is to get PackageManager:
PackageManager mPackageManager=getPackageManager();try {PackageInfo pInfo=mPackageManager.getPackageInfo(getPackageName(),0);mPackageName=pInfo.packageName;} catch (NameNotFoundException e) {e.printStackTrace();}
Next
// Returns a list of all application packages installed on the device.
List <ApplicationInfo> installList = mPackageManager. getInstalledApplications (PackageManager. GET_UNINSTALLED_PACKAGES); for (int I = 0; I <installList. size (); I ++) {ApplicationInfo = installList. get (I); if (info. packageName. equals (mPackageName) {// If the packagename is the same as the packagename of the current apk, you can get its size. String dir = info. publicSourceDir; int size = Integer. valueOf (int) new File (dir ). length (); BigDecimal apkSize = parseApkSize (size); tvSize. setText ("apk size:" + apkSize + "M"); // the size of the last displayed application }}
Java retains two decimal places:
private BigDecimal parseApkSize(int size) {BigDecimal bd = new BigDecimal((double)size/(1024*1024)); BigDecimal setScale = bd.setScale(2, BigDecimal.ROUND_DOWN);return setScale;}
How does android get the file size?
I would like to ask the landlord how to obtain the file size if the file or folder cannot be read or written? Or. length? It seems that a null pointer error will be reported.
How to reduce the size of the apk package android
The apk size is directly related to the file size of your project. The most closely related is the image size. If you need to adapt to multiple mobile phones for one apk, you 'd better create only one set of images. In addition, the image size should be compressed to ensure the quality of the image display, optimize image storage space as much as possible