java - 带图标的主屏幕快捷方式

标签 java android icons homescreen

我正在尝试在 Android 上以编程方式创建主屏幕快捷方式。到目前为止,我已经能够使用以下代码添加快捷方式本身:

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);

但是,快捷方式是使用我的资源中的默认图标安装的。但是,我想从我的网站获取图标并将图标添加到快捷方式。 首先,我需要下载这个快捷方式。假设我已完成此操作,并且图标在 sdcard 上,例如,我无法设置可绘制图标。

以下代码:

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();
    }

该文件肯定存在,我已经验证使用 adb shell...这段代码显示以下错误:

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

我做错了什么?

谢谢

最佳答案

您正在尝试从本地资源获取位图(通过使用内容提供程序)。

要从服务器下载位图,您应该遵循以下步骤:

Why is this image bitmap not downloading in Android?

关于java - 带图标的主屏幕快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7754953/

相关文章:

html - 存在文本时在输入框中插入复选图标

java - 存储和检索数据的成本

java - Google App Engine Multi-Tenancy API

java - 在方法内嵌套 while 循环后无法返回值

android - 当应用程序处于后台时, Activity 在“最近”中不可见

html - Font Awesome 菜单图标有 1px 偏移量

java - Quaere - 有人在用吗? (用于 Java 的 LINQ to 对象)

android - 如何在 Android 中设置 CPU 频率?

java - 无法更改操作栏背景

delphi - Delphi 11 Alexandria 中的组件图标