4

Am trying to create a homescreen shortcut programmatically on android. So far I've been able to add the shortcut itself with the following code:

Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.icon);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);

But, the shortcut is installed using the default icon in my resources. However, I would like to fetch icons from my website and adding an icon to the shortcut. First, I need to download this shortcut. Under the assumption that I have this done, and the icon is on the sdcard for example, I have been unable to set an drawable icon.

The following code:

try {
        Uri contentURI = Uri.parse("http://mnt/sdcard/mytest/test.png");        
            ContentResolver cr = mContext.getContentResolver();
            InputStream in;

    in = cr.openInputStream(contentURI);
    BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize=8;
        Bitmap thumb = BitmapFactory.decodeStream(in,null,options);
        Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName(mContext, mContext.getClass().getName());
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("someParameter", "HelloWorld 123");
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, thumb);
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    mContext.sendBroadcast(addIntent);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

The file definitely exist and I've verified that using adb shell... This piece of code shows the following error:

10-13 16:11:31.184: WARN/System.err(23273): java.io.FileNotFoundException: No content provider: /mnt/sdcard/mytest/test.png

What am I doing wrong?

Thanks

Vaandu
  • 4,857
  • 12
  • 49
  • 75
Maverick
  • 1,458
  • 2
  • 21
  • 35

2 Answers2

1

You are trying to get bitmap from local resources (by using content provider).

To download Bitmap from server you should follow this:

Why is this image bitmap not downloading in Android?

Community
  • 1
  • 1
Dariusz Bacinski
  • 8,324
  • 9
  • 38
  • 47
0

It seems like your application unable to access test.png. Make sure it exists. Maybe you can start with local storage rather than sd card.

Gökhan Barış Aker
  • 4,445
  • 5
  • 25
  • 35