Issue
I have added resource id in list. And used that resourceid to load image in ImageView. But it shows black. I have done it this way. Thanks in advance.
private void initArcMenu(ArcMenu menu, int[] itemDrawables, final ArrayList<AppInfo> appinfo) {
final int itemCount = appinfo.size();
for (int i = 0; i < itemCount; i++) {
long drawable = appinfo.get(i).getIconResourceId();
appinfo.get(0).getIconResourceId();
ImageView item = new ImageView(this);
// item.setImageResource((int) drawable);
item.setImageBitmap(BitmapFactory.decodeResource(getResources(),(int)appinfo.get(i).getIconResourceId()));
Log.e("Simple drawable",(int) drawable+"");
final int position = i;
menu.addItem(item, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(RingActivity.this, "position:" + appinfo.get(position).getAppName(), Toast.LENGTH_SHORT).show();
}
});
}
}
Solution
I think your problem is due to the fact that you get resource ID's for other apps, yet you are trying to load resources corresponding to those IDs from your own app's resources.
The culprit is this line:
item.setImageBitmap(BitmapFactory.decodeResource(getResources(),(int)appinfo.get(i).getIconResourceId()));
Specifically, getResources
call. This attempts to use your own app's resources to load the icon that you found. Instead you need to use the resources of the app, whose info you are processing. You can also simplify the rest of the line and use setResourceDrawable
instead.
I'm assuming that AppInfo
is your own class that contains information about some apps. It should contain the package name of the corresponding app. Then your code could look something like this:
String pkg = appinfo.get(i).getPackageName();
Resources res = getPackageManager().getResourcesForApplication(pkg);
item.setIconDrawable(res.getDrawable((int)appinfo.get(i).getResourceIcon());
If you are not in a descendent of Context
(i.e. not in an Activity or similar), then you'll need to call getPackageManager()
on an instance of Context
.
Answered By - Aleks G
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.