问题
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
回答1:
It is faster & more memory efficient to create a new ImageSource that loads the icon as its original Drawable vs. converting to a Bitmap, byte array, etc... Also this works correctly with recycling cells in a ListView, etc...
ImageSource Implementation (exists in a .NetStd/Forms library):
[TypeConverter(typeof(PackageNameSourceConverter))]
public sealed class PackageNameSource : ImageSource
{
public static readonly BindableProperty PackageNameProperty = BindableProperty.Create(nameof(PackageName), typeof(string), typeof(PackageNameSource), default(string));
public static ImageSource FromPackageName(string packageName)
{
return new PackageNameSource { PackageName = packageName };
}
public string PackageName
{
get { return (string)GetValue(PackageNameProperty); }
set { SetValue(PackageNameProperty, value); }
}
public override Task<bool> Cancel()
{
return Task.FromResult(false);
}
public override string ToString()
{
return $"PackageName: {PackageName}";
}
public static implicit operator PackageNameSource(string packageName)
{
return (PackageNameSource)FromPackageName(packageName);
}
public static implicit operator string(PackageNameSource packageNameSource)
{
return packageNameSource != null ? packageNameSource.PackageName : null;
}
protected override void OnPropertyChanged(string propertyName = null)
{
if (propertyName == PackageNameProperty.PropertyName)
OnSourceChanged();
base.OnPropertyChanged(propertyName);
}
}
TypeConverter Implementation (exists in a .NetStd/Forms library):
[TypeConversion(typeof(PackageNameSource))]
public sealed class PackageNameSourceConverter : TypeConverter
{
public override object ConvertFromInvariantString(string value)
{
if (value != null)
return PackageNameSource.FromPackageName(value);
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(PackageNameSource)));
}
}
IImageSourceHandler, IImageViewHandler Implementation (exists in a Xamarin.Android project):
public class PackageNameSourceHandler : IImageSourceHandler, IImageViewHandler
{
public async Task<Bitmap> LoadImageAsync(ImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
{
var packageName = ((PackageNameSource)imagesource).PackageName;
using (var pm = Application.Context.PackageManager)
using (var info = pm.GetApplicationInfo(packageName, PackageInfoFlags.MetaData))
using (var drawable = info.LoadIcon(pm))
{
Bitmap bitmap = null;
await Task.Run(() =>
{
bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
using (var canvas = new Canvas(bitmap))
{
drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
drawable.Draw(canvas);
}
});
return bitmap;
}
}
public Task LoadImageAsync(ImageSource imagesource, ImageView imageView, CancellationToken cancellationToken = default(CancellationToken))
{
var packageName = ((PackageNameSource)imagesource).PackageName;
using (var pm = Application.Context.PackageManager)
{
var info = pm.GetApplicationInfo(packageName, PackageInfoFlags.MetaData);
imageView.SetImageDrawable(info.LoadIcon(pm));
}
return Task.FromResult(true);
}
}
Note: Register this at the assembly level:
[assembly: ExportImageSourceHandler(typeof(PackageNameSource), typeof(PackageNameSourceHandler))]
Now in your dependency service return a list of Android applications that includes at least the PackageName, something like an IList<Package>
public class Package
{
public string Name { get; set; }
public string PackageName { get; set; }
}
Xaml Example:
Now you can bind that IList<Package> to a ListView custom cell using this PackageNameSource:
<Image WidthRequest="60" HeightRequest="60">
<Image.Source>
<local:PackageNameSource PackageName="{Binding PackageName}" />
</Image.Source>
</Image>
来源:https://stackoverflow.com/questions/53137503/xamarin-forms-list-app-with-icon-android-only