记录: 获取 APK 文件 图标

本文介绍了一种通过反射机制使用PackageParser解析APK文件并获取应用图标的方法。此过程包括加载APK、解析包信息、利用AssetManager加载资源及释放资源等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	private Bitmap getAPKIcon(String apkPath) {
        String PATH_PackageParser = "android.content.pm.PackageParser";  
        String PATH_AssetManager = "android.content.res.AssetManager";  
        try {  
            Class<?> pkgParserCls = Class.forName(PATH_PackageParser);  
            Class<?>[] typeArgs = {String.class};  
            Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);  
            Object[] valueArgs = {apkPath};  
            Object pkgParser = pkgParserCt.newInstance(valueArgs);  
              
            DisplayMetrics metrics = new DisplayMetrics();  
            metrics.setToDefaults();
            typeArgs = new Class<?>[]{File.class,String.class,  
                                    DisplayMetrics.class,int.class};  
            Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);  
              
            valueArgs=new Object[]{new File(apkPath),apkPath,metrics,0};  
              
            Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser,valueArgs);  
              
            if (pkgParserPkg==null) {  
                return null;  
            }  
            Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");  
              
            if (appInfoFld.get(pkgParserPkg)==null) {  
                return null;  
            }  
            ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);           
              
            Class<?> assetMagCls = Class.forName(PATH_AssetManager);            
            Object assetMag = assetMagCls.newInstance(); 
            typeArgs = new Class[1];  
            typeArgs[0] = String.class;  
            Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath", typeArgs);  
            valueArgs = new Object[1];  
            valueArgs[0] = apkPath;  
            assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);  
              
            Resources res = this.getResources();  
            typeArgs = new Class[3];  
            typeArgs[0] = assetMag.getClass();  
            typeArgs[1] = res.getDisplayMetrics().getClass();  
            typeArgs[2] = res.getConfiguration().getClass();  
            Constructor<Resources> resCt = Resources.class.getConstructor(typeArgs);  
            valueArgs = new Object[3];  
            valueArgs[0] = assetMag;  
            valueArgs[1] = res.getDisplayMetrics();  
            valueArgs[2] = res.getConfiguration();  
            res = (Resources) resCt.newInstance(valueArgs);  
    
            if (info!=null) {  
                if (info.icon != 0) {
                	BitmapFactory.Options options = new BitmapFactory.Options();
                	options.inSampleSize = 2;
                	Bitmap micon = null;
                	micon = BitmapFactory.decodeResource(res, info.icon, options);
                	info = null;
                	res = null;
                	pkgParserPkg = null;
                	System.gc();
                    return micon;
                }   
            }else {  
                return null;  
            }
        } catch (Exception e) {   
            e.printStackTrace();  
        }
		return null;  
        
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值