Friday, 16 November 2018

Xamarin.Forms List App with Icon (Android Only)

I have an Xamarin Forms project and i try to have a list of Installed applications with the icon.

For now i have the Label, but i dont get the image.

I use the Dependency Service to get the list of applications. :

public IEnumerable<IAppListServiceApplication> GetApplications()
        {
            var apps = Android.App.Application.Context.PackageManager.GetInstalledApplications(PackageInfoFlags.MatchAll);
            foreach (var app in apps)
            {
                var label = app.LoadLabel(Android.App.Application.Context.PackageManager);
                if (!label.ToLower().StartsWith("com."))
                    yield return new AppListServiceApplication
                    {
                        Label = label,
                        PackageName = app.PackageName,
                        AppIcon = GetAppIcon(app)
                    };
            }
        }

But i'm not able to convert the Android Drawable object to an ImageSource in the Xamarin Forms project. Here's what i try for now :

public ImageSource GetAppIcon(ApplicationInfo app)
        {
            try
            {
                var drawable = app.LoadIcon(Android.App.Application.Context.PackageManager);

                //return ImageSource.FromStream(() => new MemoryStream(bytes));
                Bitmap icon = drawableToBitmap(drawable);

                return ImageSource.FromStream(() => MemoryStreamFromBitmap(icon));
            }
            catch (Exception ex)
            {
                var message = ex.ToString();
                return null;
            }


        }
        MemoryStream MemoryStreamFromBitmap(Bitmap bmp)
        {
            MemoryStream stream = new MemoryStream();
            bmp.Compress(Bitmap.CompressFormat.Png, 0, stream);
            return stream;
        }
        Bitmap drawableToBitmap(Drawable drawable)
        {
            Bitmap bitmap = null;

            if (drawable is BitmapDrawable) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
                if (bitmapDrawable.Bitmap != null)
                {
                    return bitmapDrawable.Bitmap;
                }
            }
            if (drawable is AdaptiveIconDrawable)
            {
                AdaptiveIconDrawable adaptiveIconDrawable = (AdaptiveIconDrawable)drawable;
                if (!(adaptiveIconDrawable.IntrinsicWidth <= 0 || adaptiveIconDrawable.IntrinsicHeight <= 0))
                {
                    bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
                    Canvas c = new Canvas(bitmap);
                    drawable.SetBounds(0, 0, c.Width, c.Height);
                    drawable.Draw(c);
                    return bitmap;

                }
            }

            if (drawable.IntrinsicWidth <= 0 || drawable.IntrinsicHeight <= 0)
            {
                bitmap = Bitmap.CreateBitmap(1, 1, Bitmap.Config.Argb8888); // Single color bitmap will be created of 1x1 pixel
            }
            else
            {
                bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
            }

            Canvas canvas = new Canvas(bitmap);
            drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
            drawable.Draw(canvas);
            return bitmap;
        }

As you can see, i'm new in xamarin and Android ^^ . I did not find on Google how to pass a Drawable from Android to display it in Xamarin Forms.

If you can point me the right direction to leard how it can be done.

Tanks

EDIT: Add output error

I get this error in the output window:

ImageLoaderSourceHandler: Image data was invalid: Xamarin.Forms.StreamImageSource
11-05 04:31:19.287 D/skia    (10270): --- SkAndroidCodec::NewFromStream returned null

**EDIT: ** Add Project

Here is the problem in gitHub : https://github.com/werddomain/Xamarin-Android-Laucher/tree/master/Forms/AndroidCarLaucher



from Xamarin.Forms List App with Icon (Android Only)

No comments:

Post a Comment