using System.IO; using System.Threading; using System.Threading.Tasks; using Android.Content; using Android.Graphics; using Xamarin.Forms.Internals; namespace Xamarin.Forms.Platform.Android { public sealed class FileImageSourceHandler : IImageSourceHandler { // This is set to true when run under designer context internal static bool DecodeSynchronously { get; set; } public async Task LoadImageAsync(ImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken)) { string file = ((FileImageSource)imagesource).File; Bitmap bitmap; if (File.Exists (file)) bitmap = !DecodeSynchronously ? (await BitmapFactory.DecodeFileAsync (file).ConfigureAwait (false)) : BitmapFactory.DecodeFile (file); else bitmap = !DecodeSynchronously ? (await context.Resources.GetBitmapAsync (file).ConfigureAwait (false)) : context.Resources.GetBitmap (file); if (bitmap == null) { Log.Warning(nameof(FileImageSourceHandler), "Could not find image or image file was invalid: {0}", imagesource); } return bitmap; } } }