AppDetail
public class AppDetail {
private CharSequence name; // Ex: com.google.youtube
private CharSequence label; // Ex: Youtube
private Drawable icon;
// Getters and setters
...
}
AllAppsActivity //在这里,我可以获得所有应用程序图标
private void loadAllApps() {
packageManager = this.getPackageManager();
appDetailList = new ArrayList<AppDetail>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = packageManager.queryIntentActivities(i, 0);
for (ResolveInfo ri : availableActivities) {
AppDetail appDetail = new AppDetail();
appDetail.setLabel(ri.loadLabel(packageManager));
appDetail.setName(ri.activityInfo.packageName);
appDetail.setIcon(ri.activityInfo.loadIcon(packageManager));
appDetailList.add(appDetail);
}
}
现在我想使用Picasso加载所有图标,但Picasso需要File / String path / int resouce ID / Uri uri for load()函数 我该怎么办?提前致谢
答案 0 :(得分:5)
我知道这已经过时了,但我想我会为遇到这个问题的其他人分享我的解决方案。您可以使用自定义RequestHandler
。以下是我使用RequestHandler
加载应用图标的Picasso
:
public class AppIconRequestHandler extends RequestHandler {
public static final String SCHEME_PNAME = "pname";
private final PackageManager pm;
private final int dpi;
private Bitmap defaultAppIcon;
public AppIconRequestHandler(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
dpi = am.getLauncherLargeIconDensity();
pm = context.getPackageManager();
}
@Override public boolean canHandleRequest(Request data) {
return data.uri != null && TextUtils.equals(data.uri.getScheme(), SCHEME_PNAME);
}
@Override public Result load(Request request, int networkPolicy) throws IOException {
try {
return new Result(getFullResIcon(request.uri.toString().split(":")[1]), DISK);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
private Bitmap getFullResIcon(String packageName) throws PackageManager.NameNotFoundException {
return getFullResIcon(pm.getApplicationInfo(packageName, 0));
}
private Bitmap getFullResIcon(ApplicationInfo info) {
try {
Resources resources = pm.getResourcesForApplication(info.packageName);
if (resources != null) {
int iconId = info.icon;
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return getFullResDefaultActivityIcon();
}
private Bitmap getFullResIcon(Resources resources, int iconId) {
final Drawable drawable;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
drawable = resources.getDrawableForDensity(iconId, dpi, null);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
drawable = resources.getDrawableForDensity(iconId, dpi);
} else {
drawable = resources.getDrawable(iconId);
}
} catch (Resources.NotFoundException e) {
return getFullResDefaultActivityIcon();
}
return drawableToBitmap(drawable);
}
private Bitmap getFullResDefaultActivityIcon() {
if (defaultAppIcon == null) {
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
drawable = Resources.getSystem().getDrawableForDensity(
android.R.mipmap.sym_def_app_icon, dpi);
} else {
drawable = Resources.getSystem().getDrawable(
android.R.drawable.sym_def_app_icon);
}
defaultAppIcon = drawableToBitmap(drawable);
}
return defaultAppIcon;
}
}
只需将RequestHandler
添加到您的Picasso
实例:
Picasso.setSingletonInstance(new Picasso.Builder(this)
.addRequestHandler(new AppIconRequestHandler(this))
.build());
然后,使用Uri
中使用的方案创建RequestHandler
:
Uri uri = Uri.parse(AppIconRequestHandler.SCHEME_PNAME + "com.facebook.katana");
Picasso.with(context).load(uri).into(imageView);