Homescreen shortcuts with icons

核能气质少年 提交于 2019-11-29 15:21:06

问题


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


回答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?




回答2:


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.



来源:https://stackoverflow.com/questions/7754953/homescreen-shortcuts-with-icons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!