APK
/**
* 获取Apk文件的Log图标
* @param context
* @param apk_path Apk路径
* @return
*/
public static Drawable getApkThumbnail(Context context, String apk_path){
if(context == null){
return null;
}
try{
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path, PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
/**获取apk的图标 */
appInfo.sourceDir = apk_path;
appInfo.publicSourceDir = apk_path;
if(appInfo != null){
Drawable apk_icon = appInfo.loadIcon(pm);
return apk_icon;
}
}catch(Exception e){
}
return null;
}
得到的是Drawable,转换成BitMap
/**
* Drawable转Bitmap
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable){
if(drawable == null){
return null;
}
// 取 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;
}
视频文件
bitmap = ScreenshotUtils.createVideoThumbnail(filePath);
bitmap = ScreenshotUtils.extractThumbnail(bitmap, 100, 100);
public class ScreenshotUtils {
/**
* 创建缩略图
*
* @param filePath
* @return
*/
public static Bitmap createVideoThumbnail(String filePath){
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MICRO_KIND);
return bitmap;
}
/**
* 将图片转换成指定宽高
*
* @param source
* @param width
* @param height
* @return
*/
public static Bitmap extractThumbnail(Bitmap source, int width, int height){
Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, width, height);
return bitmap;
}
}