Question

I already wrote a working code for showing a list of all available shortcuts from all apps and capturing the intent for the chosen shortcut as weell as the name of the app and the shortcut. But I can't seem to find how to get the shortcut icon. In the onActivityResult I tried the data.getStringExtra(Intent.EXTRA_SHORTCUT_ICON) function but this produces a string which returns empty. Can someone help?

(I want the shortcut icon...not the app icon)

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Log.w("on activity result ston action picker","aaaa");
    if(resultCode == Activity.RESULT_OK && data != null) {
        switch (requestCode){
        case pick_shortcut:
                Intent j = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
                String shortcut_name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
                ComponentName componentName2 = j.getComponent();
                String packageName2 = componentName2.getPackageName();
                PackageManager packageManager2 =  getPackageManager();
                ApplicationInfo applicationInfo2 = null;
                try {
                    applicationInfo2 = packageManager2.getApplicationInfo(packageName2, 0);
                } catch (final NameNotFoundException e) {}
                String title2 = (String)((applicationInfo2 != null) ? packageManager2.getApplicationLabel(applicationInfo2) : "???");
                Log.w(shortcut_name,title2);
           }
       }
    }
Was it helpful?

Solution

You was almost there.

Intent.EXTRA_SHORTCUT_ICON should be a pacelled Bitmap, not a String.

So instead of using:

data.getStringExtra(Intent.EXTRA_SHORTCUT_ICON)

You should be looking at something like:

Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);


EDIT:

As a side note, if your dealing with third party applications, EXTRA_SHORTCUT_ICON could be null, and EXTRA_SHORTCUT_ICON_RESOURCE may be passed instead.

EXTRA_SHORTCUT_ICON_RESOURCE is a ShortcutIconResource, which is a class that contains two Strings: packageName and resourceName.

(These could be used to load the third party app's resources, then grab the Bitmap resource using the resource name.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top