Xamarin 为快捷方式设置自定义图标 [英] Xamarin set custom Icon for Shortcut

查看:43
本文介绍了Xamarin 为快捷方式设置自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码,但这不起作用.没有例外,但自定义图标不起作用.我不想使用Resource.Drawable.example_icon_bla_bla"我想从字节 [] 或文件中设置图标.... 我的 Android 版本是 6.0.1,API 级别是 23

I use this code but this does not work. There are no exceptions but custom icon does not work. I don't want to use "Resource.Drawable.example_icon_bla_bla" I want to set icon from byte[] or from File.... My Android version is 6.0.1 and API Level is 23

private void AddShortcut(string appName, string url, byte[] icon_byte)
        {
            try
            {
                var uri = Android.Net.Uri.Parse(url);
                var intent_ = new Intent(Intent.ActionView, uri);
                if (icon_byte.Length > 0)
                {
                   
                  
                    var intent = new Intent();
                    intent.PutExtra(Intent.ExtraShortcutIntent, intent_);
                    intent.PutExtra(Intent.ExtraShortcutName, appName);

                    intent.PutExtra(Intent.ExtraShortcutIcon,Icon.CreateWithBitmap(BitmapFactory.DecodeFile(
                        Android.OS.Environment.ExternalStorageDirectory + "/Duck.png"))); //this custom Icon line doesn't work.

                    //intent.PutExtra("duplicate", false);
                    intent.SetAction("com.android.launcher.action.INSTALL_SHORTCUT");
                    SendBroadcast(intent);
                    Toast.MakeText(this, "Added", ToastLength.Long).Show();
                }
            }
            catch (Exception ex) { Toast.MakeText(this, ex.Message, ToastLength.Long).Show(); }
        }

推荐答案

我已经修复了这一行;

    private void AddShortcut(string appName, string url, byte[] icon_byte)
            {
                try
                {
                    Bitmap bitmap = BitmapFactory.DecodeByteArray(icon_byte,0,icon_byte.Length); 
                     // 72x72 best size for launcher icon.
    
                    var uri = Android.Net.Uri.Parse(url);
                    var intent_ = new Intent(Intent.ActionView, uri);
    
                    Intent installer = new Intent();
                    installer.PutExtra("android.intent.extra.shortcut.INTENT", intent_);
                    installer.PutExtra("android.intent.extra.shortcut.NAME", appName);
                    installer.PutExtra("android.intent.extra.shortcut.ICON", bitmap);
                    installer.SetAction("com.android.launcher.action.INSTALL_SHORTCUT");
                    SendBroadcast(installer);
                }
                catch (Exception) { 
                    //Toast.MakeText(this,"Shortcut: " +ex.Message, ToastLength.Long).Show(); 
                }
    
            }

这篇关于Xamarin 为快捷方式设置自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆