How does android get its own apk size ?, Get apk for android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.