get Package Size In Bytes - Android App

Android examples for App:Package

Description

get Package Size In Bytes

Demo Code


//package com.java2s;
import java.io.File;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;

import android.content.pm.PackageManager.NameNotFoundException;

public class Main {

    public static final long getPackageSizeInBytes(Context context,
            String packageName) {
        final ApplicationInfo appInfo = getApplicationInfo(context,
                packageName);//from w  ww .  ja  v a 2 s .co m
        if (appInfo == null) {
            return -1L;
        }

        final File file = new File(appInfo.sourceDir);
        return file.length();
    }

    public static ApplicationInfo getApplicationInfo(Context context) {
        final PackageInfo pkgInfo = getPackageInfo(context,
                context.getPackageName());
        return (pkgInfo != null) ? pkgInfo.applicationInfo : null;
    }

    public static ApplicationInfo getApplicationInfo(Context context,
            String packageName) {
        final PackageInfo pkgInfo = getPackageInfo(context, packageName);
        return (pkgInfo != null) ? pkgInfo.applicationInfo : null;
    }

    public static final PackageInfo getPackageInfo(Context context) {
        return getPackageInfo(context, context.getPackageName(), 0);
    }

    public static final PackageInfo getPackageInfo(Context context,
            String packageName) {
        return getPackageInfo(context, packageName, 0);
    }

    public static final PackageInfo getPackageInfo(Context context,
            int flags) {
        return getPackageInfo(context, context.getPackageName(), flags);
    }

    public static final PackageInfo getPackageInfo(Context context,
            String packageName, int flags) {
        PackageInfo info = null;
        try {
            info = context.getPackageManager().getPackageInfo(packageName,
                    flags);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }
}

Related Tutorials