原因:
写这个的原因就是网上大都是告诉你如何用drawable里的资源做快捷方式,那么本地的图片能不能做快捷方式呢?肯定是可以的,所以我找了下安卓的源码,发现有这样的介绍
/**
* Activity Action: Creates a shortcut.
* <p>Input: Nothing.</p>
* <p>Output: An Intent representing the shortcut. The intent must contain three
* extras: SHORTCUT_INTENT (value: Intent), SHORTCUT_NAME (value: String),
* and SHORTCUT_ICON (value: Bitmap) or SHORTCUT_ICON_RESOURCE
* (value: ShortcutIconResource).</p>
*
* @see #EXTRA_SHORTCUT_INTENT
* @see #EXTRA_SHORTCUT_NAME
* @see #EXTRA_SHORTCUT_ICON
* @see #EXTRA_SHORTCUT_ICON_RESOURCE
* @see android.content.Intent.ShortcutIconResource
*/
不难看出有两种方式创建快捷方式图标,不过大都用的第一种。所以我们就试试第二种吧。
解决方案:
既然都看了源码的介绍了,自然就知道怎么做了。
public void CreateShotCast(Context context, Class<?> clazz) {
Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClass(context, clazz);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "测试");
// Intent.EXTRA_SHORTCUT_ICON 是bitmap对象
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeFile("路径"));
context.sendBroadcast(shortcut);
}