对于已安装的应用我们可以使用PackageManager获取其图标 程序版本 版本名称 应用名 程序的权限 程序的签名等等。
获取安装已安装应用信息
直接上代码:
public class Info {
Context context;
PackageManager pm;
public Info(Context context) {
this.context = context;
pm = context.getPackageManager();
}
/*
* 获取程序 图标
*/
public Drawable getAppIcon(String packname) {
try {
ApplicationInfo info = pm.getApplicationInfo(packname, 0);
return info.loadIcon(pm);
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*
*获取程序的版本号
*/
public String getAppVersion(String packname) {
try {
PackageInfo packinfo = pm.getPackageInfo(packname, 0);
return packinfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/*
* 获取程序的名字
*/
public String getAppName(String packname) {
try {
ApplicationInfo info = pm.getApplicationInfo(packname, 0);
return info.loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*
* 获取程序的权限
*/
public String[] getAppPremission(String packname) {
try {
PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
//获取到所有的权限
return packinfo.requestedPermissions;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/*
* 获取程序的签名
*/
public String getAppSignature(String packname) {
try {
PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);
//获取到所有的权限
return packinfo.signatures[0].toCharsString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
保存获取的图标
void getIconByPackagename(Context mContext)
{
String packagename = mEditText.getText().toString();
Info info = new Info(getApplicationContext());
String version = "appname: "+info.getAppName(packagename)+"\n"
+"version: "+info.getAppVersion(packagename)+"\n";
Log.e(TAG,"packagename:"+packagename + version);
Drawable d = info.getAppIcon(packagename);
if(d == null){
Log.e(TAG,"Error: getAppIcon "+packagename);
return ;
}
Bitmap bitmap = drawableToBitmap(d);
Bitmap bitmapCuted = cutFromBitmap(bitmap);
// saveBitmap(bitmapCuted,packagename+".png");
mTextView.setText(version);
imageView.setBackground(d);
//imageView.setBackground(new BitmapDrawable(bitmapCuted));
Toast.makeText(mContext, packagename+" " +getBitmapsize2(bitmapCuted), Toast.LENGTH_SHORT).show();
}
Drawable与Bitmap的对比
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。
Drawable - 作为Android平台下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
Drawable在内存占用和绘制速度这两个非常关键的点上胜过Bitmap
Drawable与Bitmap之间的转换
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
图片的剪切
private static final int ICON_LEN_LDPI120 =36;
private static final int ICON_LEN_MDPI160 =48;
private static final int ICON_LEN_HDPI240 =72;
private static final int ICON_LEN_XHDPI320 =96;
private static final int ICON_LEN_XXHDPI480 =144;
//需要切割的图片资源、切割起始点的X坐标、切割起始点的Y坐标、切割多宽、切割多高
public Bitmap cutFromBitmap(Bitmap bitmap) {
int starx=(90-72)/2;
int stary=starx;
int len;
int oLen=bitmap.getWidth();
switch (densityDpi) {
case ICON_LEN_LDPI120: len =ICON_LEN_LDPI120; break;
case ICON_LEN_MDPI160: len =ICON_LEN_MDPI160; break;
case ICON_LEN_HDPI240: len =ICON_LEN_HDPI240; break;
case ICON_LEN_XHDPI320: len =ICON_LEN_XHDPI320; break;
case ICON_LEN_XXHDPI480: len =ICON_LEN_XXHDPI480; break;
default: return bitmap;
}
if(oLen < len){
Log.e(TAG,"ERROR:oLen < len");
return bitmap;
}
Bitmap mm = Bitmap.createBitmap(bitmap,(oLen-len)/2,(oLen-len)/2,len,len);
return mm;
}
图片的保存
public void saveBitmap(Bitmap bm,String picName ) {
Log.e(TAG, "保存图片");
File f = new File("/sdcard/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.i(TAG, "已经保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
参考链接
- Android中 Bitmap和Drawable相互转换的方法
- Android 一个apk多个ICON执行入口
- Android,App 常用图标尺寸规范