close
public static Bitmap getApkIcon(Context context, String path) {
Bitmap icon = null;
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageArchiveInfo(path, 0);
// if the apk file is broken, then packageInfo will be null
if (packageInfo == null) {
return null;
}
ApplicationInfo appInfo = packageInfo.applicationInfo;
//icon = pm.getApplicationLogo(appInfo);
AssetManager assetMgr = reflectionApis.getAssetManagerWithPath(path); // 拿到別的APK的AssetManager
if (null != assetMgr) {
Resources r = context.getResources(); // 取得一個實作Resource介面的實例後,可使用getFile()、getInputStream()等方式來操作或取得資源檔案的相關資源
Resources res = new Resources(assetMgr, r.getDisplayMetrics(), r.getConfiguration());
if (appInfo.icon > 0) {
Drawable drawable = ResourcesCompat.getDrawable(res, appInfo.icon, context.getTheme()); // 從appInfo中要icon並套用目前主題
if (drawable instanceof BitmapDrawable) { // 若是BitmapDrawable,直接getBitmap
icon = ((BitmapDrawable) drawable).getBitmap();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& drawable instanceof AdaptiveIconDrawable) {// 先判斷是否為8.0後版本,以免前面版本不認得AdaptiveIconDrawable造成Crash
drawable為切成圓形的adaptive icon,若要自己切形狀則用layerDrawable疊起來在處理
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
icon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} else {
icon = BitmapFactory.decodeResource(res, appInfo.icon);
}
}
assetMgr.close();
}
return icon;
}
}
getIntrinsicWidth用途:
http://lp43.blogspot.tw/2012/03/android.html
http://www.voidcn.com/article/p-wtmtfvkm-nc.html
http://yezimianbao.iteye.com/blog/2068655
全站熱搜