Java Code Examples for android.content.pm.PackageManager.getPackageArchiveInfo()

The following are Jave code examples for showing how to use getPackageArchiveInfo() of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: easyfilemanager   File: IconUtils.java   View Source Code Vote up 6 votes
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){
 	int icon =  sMimeIcons.get(mimeType);
     if (path != null) {
         final PackageManager pm = context.getPackageManager();
try {
	final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
	if (packageInfo != null) {
		packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path;
		// know issue with nine patch image instead of drawable
		return pm.getApplicationIcon(packageInfo.applicationInfo);
	}
} catch (Exception e) {
	return ContextCompat.getDrawable(context, icon);
}
     } else {
         return ContextCompat.getDrawable(context, icon);
     }
     return null;
 }
 
Example 2
Project: editor-sql   File: FeThumbUtils.java   View Source Code Vote up 6 votes
public static Bitmap getLocalApkIcon(PackageManager pm, String apkPath) {
    Drawable icon;
    Bitmap iconBmp;
    try {
        PackageInfo pi = pm.getPackageArchiveInfo(apkPath,
                PackageManager.GET_ACTIVITIES);
        pi.applicationInfo.sourceDir = apkPath;
        pi.applicationInfo.publicSourceDir = apkPath;
        icon = pi.applicationInfo.loadIcon(pm);
        iconBmp = drawableToBitmap(icon);
    } catch (Exception e) {
        return null;
    }

    return iconBmp;
}
 
Example 3
Project: XDisplay   File: Util.java   View Source Code Vote up 6 votes
/**
 * from apk file get apk icon bitmap.
 * @param context
 * @param apkPath
 * @return
 */
public static Bitmap getApkIconThumbnail(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    Drawable drawable = null;
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            drawable = appInfo.loadIcon(pm);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }
    if(drawable!=null){
        return drawableToBitmap(drawable);
    }
    return null;
}
 
Example 4
Project: Android-Skin   File: PackageUtil.java   View Source Code Vote up 6 votes
public static String getApkPackageName(Context context,String apkPath) {
    try {
        if(context==null)
            return null;
        PackageManager pm = context.getPackageManager();
        if(SkinStringUtils.isNull(apkPath)){
            pm = context.getPackageManager();
        }
        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        ApplicationInfo appInfo = null;
        if (info != null) {
            appInfo = info.applicationInfo;
            String packageName = appInfo.packageName;
            return packageName;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
 
Example 5
Project: FireFiles   File: IconUtils.java   View Source Code Vote up 6 votes
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){
 	int icon =  sMimeIcons.get(mimeType);
     if (path != null) {
         final PackageManager pm = context.getPackageManager();
try {
	final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
	if (packageInfo != null) {
		packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path;
		// know issue with nine patch image instead of drawable
		return pm.getApplicationIcon(packageInfo.applicationInfo);
	}
} catch (Exception e) {
	return ContextCompat.getDrawable(context, icon);
}
     } else {
         return ContextCompat.getDrawable(context, icon);
     }
     return null;
 }
 
Example 6
Project: simple-share-android   File: IconUtils.java   View Source Code Vote up 6 votes
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){
 	int icon =  sMimeIcons.get(mimeType);
     if (path != null) {
         final PackageManager pm = context.getPackageManager();
try {
	final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
	if (packageInfo != null) {
		packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path;
		// know issue with nine patch image instead of drawable
		return pm.getApplicationIcon(packageInfo.applicationInfo);
	}
} catch (Exception e) {
	return ContextCompat.getDrawable(context, icon);
}
     } else {
         return ContextCompat.getDrawable(context, icon);
     }
     return null;
 }
 
Example 7
Project: ClassLoader   File: OpenResourceActivity.java   View Source Code Vote up 6 votes
public String getApkInfo(String fileName) {
    try {
        String dexPath = null;
        if (getExternalFilesDir(null) != null) {
            dexPath = new File(getExternalFilesDir(null), fileName).getAbsolutePath();
        } else if (getFilesDir() != null) {
            dexPath = new File(getFilesDir(), fileName).getAbsolutePath();
        }

        PackageManager pm = getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(dexPath, 0);

        StringBuilder sb = new StringBuilder();
        sb.append("\n*** Apk info ***\n");
        sb.append("versionCode:" + info.versionCode);
        sb.append("\nversionName:" + info.versionName);
        sb.append("\n*** Apk info ***\n");

        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return e.toString();
    }
}
 
Example 8
Project: android-apkbox   File: ApkLoader.java   View Source Code Vote up 6 votes
/**
 * parserApkOrmModel
 *
 * @param context context
 * @param apkPath apkPath
 * @return model
 */
public static ApkOrmModel parserApkOrmModel(Context context, String apkPath) {
    File file = new File(apkPath);
    if (file.exists() && apkPath.endsWith(".apk")) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pkg = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
            if (pkg != null) {
                ApkOrmModel model = new ApkOrmModel();
                model.setApkPath(apkPath);
                model.setApkPackageName(pkg.packageName);
                model.setApkVersionCode(pkg.versionCode);
                model.setApkVersionName(pkg.versionName);
                model.setApkSignture(ApkSignture.getSignture(context, apkPath));
                model.setApkSha1(ApkMd5.sha1(file));
                return model;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
 
Example 9
Project: LiveGiftLayout   File: FileUtil.java   View Source Code Vote up 6 votes
public static Drawable getApkIcon(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            return appInfo.loadIcon(pm);
        } catch (OutOfMemoryError e) {
            Log.e(LOG_TAG, e.toString());
        }
    }
    return null;
}
 
Example 10
Project: GitHub   File: ApkUtils.java   View Source Code Vote up 5 votes
/** 根据文件路径获取包名 */
public static String getPackageName(Context context, String filePath) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo info = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        return appInfo.packageName;  //得到安装包名称
    }
    return null;
}
 
Example 11
Project: Bailan   File: AppInfoUtils.java   View Source Code Vote up 5 votes
public static PackageInfo getPackageInfo(Context context, String apkFilepath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pkgInfo = null;
    try {
        pkgInfo = pm.getPackageArchiveInfo(apkFilepath, PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
    } catch (Exception e) {
        // should be something wrong with parse
        e.printStackTrace();
    }
    return pkgInfo;
}
 
Example 12
Project: AndroidDevSamples   File: ApplicationUtil.java   View Source Code Vote up 5 votes
@CheckResult
public static boolean checkApk(Context context, @NonNull String apkPath) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            return true;
        }
    } catch (Exception e) {
        Toast.makeText(context, "安装包失效", Toast.LENGTH_SHORT).show();
    }
    return false;
}
 
Example 13
Project: GongXianSheng   File: CommonUtils.java   View Source Code Vote up 5 votes
public static String getApkVersionCode(Context context, String filePath) {
	PackageManager pm = context.getPackageManager();
	PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
	if (info != null) {
		return String.valueOf(info.versionCode);       //得到版本信息
	}
	return null;
}
 
Example 14
Project: springreplugin   File: PackageUtils.java   View Source Code Vote up 5 votes
/**
 * 获取PackageInfo对象
 * 该方法解决了Android 2.x环境不能获取签名的系统问题,故可以“全面使用”
 */
public static PackageInfo getPackageArchiveInfo(PackageManager pm, String pkgFilePath, int flags) {
    PackageInfo info = null;
    try {
        info = pm.getPackageArchiveInfo(pkgFilePath, flags);
    } catch (Throwable e) {
        if (LogDebug.LOG) {
            e.printStackTrace();
        }
    }

    if (info == null) {
        return null;
    }

    // Android 2.x的系统通过常规手段(pm.getPackageArchiveInfo)时会返回null,只能通过底层实现
    // 判断依据:1、想要签名;2、没拿到签名;3、Android 4.0以前
    if ((flags & PackageManager.GET_SIGNATURES) != 0) {
        if (info.signatures == null) {
            if (Build.VERSION.SDK_INT < 14) {
                info.signatures = getPackageArchiveSignaturesInfoAndroid2x(pkgFilePath);
            }
        }
    }

    return info;
}
 
Example 15
Project: XKik   File: Util.java   View Source Code Vote up 4 votes
public static String getKikVersion(XC_LoadPackage.LoadPackageParam lpparam, PackageManager pm) {
    String apkName = lpparam.appInfo.sourceDir;
    String fullPath = Environment.getExternalStorageDirectory() + "/" + apkName;
    PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);
    return info.versionName;
}
 
Example 16
Project: CSkin   File: SPluginUtil.java   View Source Code Vote up 4 votes
public static PackageInfo getPackageInfo(Context context, String apkPath) {

        PackageManager packageManager = context.getPackageManager();

        PackageInfo packageInfo = packageInfoMap.get(apkPath);
        if (packageInfo == null) {

            packageInfo = packageManager.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);

            packageInfoMap.put(apkPath, packageInfo);

        }


        return packageInfo;

    }
 
Example 17
Project: android-apkbox   File: ApkLoader.java   View Source Code Vote up 4 votes
/**
 * loadApk
 *
 * @param context context
 * @param apkPath apkPath
 * @return
 */
public static ApkLoaded loadApk(Context context, String apkPath) {
    File file = new File(apkPath);
    if (file.exists() && apkPath.endsWith(".apk")) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pkg = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
            if (pkg != null) {
                if (mConstructor == null) {
                    mConstructor = ApkLoaded.class.getDeclaredConstructor();
                }
                mConstructor.setAccessible(true);
                ApkLoaded loaded = mConstructor.newInstance();
                loaded.setHostContext(context.getApplicationContext());
                ApplicationInfo info = pkg.applicationInfo;
                if (Build.VERSION.SDK_INT >= 8) {
                    info.sourceDir = apkPath;
                    info.publicSourceDir = apkPath;
                }
                info.nativeLibraryDir = ApkNative.copyNativeLibrary(apkPath);
                String applicationName = ApkManifestParser.getApplicationName(apkPath);
                if (applicationName != null && !"".equals(applicationName)) {
                    applicationName = parserClassName(pkg.packageName, applicationName);
                    info.className = applicationName;
                }
                loaded.setApkPath(apkPath);
                loaded.setApkName(pm.getApplicationLabel(info).toString());
                loaded.setApkIcon(info.loadIcon(pm));
                loaded.setApkSignture(ApkSignture.getSignture(context, apkPath));
                loaded.setApkSha1(ApkMd5.sha1(file));
                loaded.setApkInfo(pkg);
                loaded.setApkVersionCode(pkg.versionCode);
                loaded.setApkVersionName(pkg.versionName);
                loaded.setApkPackageManager(pm);
                loaded.setConfigures(ApkConfigure.getConfigure(ApkResources.getAssets(context, apkPath)));
                loaded.setFilters(ApkManifestParser.getIntentFilter(apkPath));
                loaded.setApkLauncher(getMainActivityName(loaded, info.packageName));
                loaded.setPermissions(parserPermissions(context, apkPath));
                loaded.setProviders(parseProviders(apkPath));
                ApkReceiver.unregisterReceiver(loaded);
                loaded.setApkApplication(createApplication(context, loaded, applicationName));
                ApkReceiver.registerReceiver(context, loaded);
                ApkLogger.get().debug("Load apk success :" + loaded, null);
                return loaded;
            } else {
                ApkLogger.get().debug("Load apk fail : package info is null", null);
            }
        } catch (Exception e) {
            ApkLogger.get().debug("Load apk fail", e);
        }
    } else {
        ApkLogger.get().debug("Invalid apk file : " + apkPath, null);
    }
    return null;
}
 
Example 18
Project: SkinFramework   File: SkinManager.java   View Source Code Vote up 2 votes
/**
 * Get package archive info.This will cost >=500ms.
 *
 * @param skinPath Path of skin archive.
 * @return
 */
private PackageInfo getPackageInfo(String skinPath) {
    PackageManager pm = mContext.getPackageManager();
    return pm.getPackageArchiveInfo(skinPath, PackageManager.GET_CONFIGURATIONS);
}
 
Example 19
Project: Android-skin-support   File: SkinCompatManager.java   View Source Code Vote up 2 votes
/**
 * 获取皮肤包包名.
 *
 * @param skinPkgPath sdcard中皮肤包路径.
 * @return
 */
public String getSkinPackageName(String skinPkgPath) {
    PackageManager mPm = mAppContext.getPackageManager();
    PackageInfo info = mPm.getPackageArchiveInfo(skinPkgPath, PackageManager.GET_ACTIVITIES);
    return info.packageName;
}
 
Example 20
Project: Android-skin-support   File: SkinCompatManager.java   View Source Code Vote up 2 votes
/**
 * 获取皮肤包包名.
 *
 * @param skinPkgPath sdcard中皮肤包路径.
 * @return
 */
public String getSkinPackageName(String skinPkgPath) {
    PackageManager mPm = mAppContext.getPackageManager();
    PackageInfo info = mPm.getPackageArchiveInfo(skinPkgPath, PackageManager.GET_ACTIVITIES);
    return info.packageName;
}