android 获取应用图标

private Bitmap getIcon(Context context){
		Log.w("Log", "获取的本地图标---------");
		Bitmap map = null;
		PackageManager pm = context.getPackageManager(); // 获得PackageManager对象  
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);
		Collections.sort(list,new ResolveInfo.DisplayNameComparator(pm));
		for(ResolveInfo reInfo : list){
			if(context.getPackageName().equals(reInfo.activityInfo.packageName)){
				Drawable draw = reInfo.loadIcon(pm);
				map = drawableToBitmap(draw);
			}
		}
		return map;
		
	}
	
	private  Bitmap drawableToBitmap(Drawable drawable) {
		          // 取 drawable 的长宽
		          int w = drawable.getIntrinsicWidth();
		          int h = drawable.getIntrinsicHeight();
		  
		          // 取 drawable 的颜色格式
		          Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
		                  : Bitmap.Config.RGB_565;
		          // 建立对应 bitmap
		         Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		         // 建立对应 bitmap 的画布
		         Canvas canvas = new Canvas(bitmap);
		         drawable.setBounds(0, 0, w, h);
		         // 把 drawable 内容画到画布中
	         drawable.draw(canvas);
		         return bitmap;
		    }
 
 
上面获取的是系统的应用图标
 
 
如题,获取所有图标
/**获得当前应用图标 的图像**/
 private Bitmap getIcon(Context context){
 Bitmap map = null;
 PackageManager pm = context.getPackageManager(); // 获得PackageManager对象  
 // 查询所有已经安装的应用程序  
        List<ApplicationInfo> listAppcations = pm  
                .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);  
        Collections.sort(listAppcations,  
                new ApplicationInfo.DisplayNameComparator(pm));// 排序
        for(ApplicationInfo info : listAppcations){
        	if(info.packageName.equals(context.getPackageName())){
        		map = scaleImg(drawableToBitmap(info.loadIcon(pm)),32,32);
        	}
        }
 return map;
 
 }

你可能感兴趣的:(android 获取应用图标)