Baby_Bonnie 2013-02-04 03:27 采纳率: 96.4%
浏览 7831
已采纳

Android桌面的快捷方式图标不显示

我想在安卓桌面的某一位置通过代码添加了一个快捷方式,我从远程服务器上下载了图标,然后把这个图标设置成桌面的快捷方式图标。

String url = "http://www.the-brights.net/images/icons/brights_icon_50x50.gif";
    InputStream ins = null;

    try {
        ins = new java.net.URL(url).openStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bm = BitmapFactory.decodeStream(new FlushedInputStream(ins));
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;

    try {
        outStream = mContext.openFileOutput("test.png", Context.MODE_PRIVATE);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }


    Log.i("info", "Got here");
    Bitmap bm2;
    try {
        bm2 = BitmapFactory.decodeStream(mContext.openFileInput("test.png"));
        Log.i("info", bm2.toString());

        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, bm2);
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        mContext.sendBroadcast(addIntent); 
    }
    catch(Exception e) {
        e.printStackTrace();
    }

问题是快捷方式添加上了,但是图标却显示不出来。显示的是默认的快捷方式图标。 错在哪里呢?

  • 写回答

3条回答 默认 最新

  • 80后VS烦恼 2013-02-04 04:29
    关注

    你的图标应该这样获取:

    BitmapDrawable iconBitmapDrawabel = null;
    try {
                    iconBitmapDrawabel = (BitmapDrawable) getPackageManager().getActivityIcon(newIntent);//getApplicationIcon(app.activityInfo.packageName);
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                }
    
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap()); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题