带图标的主屏幕快捷方式



我试图在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);

但是,快捷方式是使用我的资源中的默认图标安装的。然而,我想从我的网站获取图标,并添加一个图标的快捷方式。首先,我需要下载这个快捷方式。假设我已经这样做了,例如,图标在sd卡上,我无法设置一个可绘制的图标。

以下代码:

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

我做错了什么?

谢谢

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

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

为什么这个图像位图不能在Android中下载?

您的应用程序似乎无法访问test.png。确保它存在。也许你可以从本地存储开始,而不是sd卡。

最新更新