public async static Task <BitmapImage> LoadBitmapAsync(string uri) { if (uri == null) { return(null); } Stream memStream = null; BitmapImage bitmap = null; if (uri.StartsWith("data:image")) { string ex; memStream = new MemoryStream(ImageUtility.DecodeImageFromDataUri(uri, out ex)); } else if (uri.StartsWith("http") || uri.StartsWith("ftp")) { memStream = new MemoryStream(); var httPeter = new HttpClient(); try { var stream = await httPeter.GetStreamAsync(uri); await stream.CopyToAsync(memStream); } catch (Exception e) { iApp.Log.Info(e); memStream = null; } } else { byte[] bytes = iApp.File.Read(uri, EncryptionMode.NoEncryption); if (bytes == null) { iApp.Log.Info("Not found: " + uri); } else { memStream = new MemoryStream(bytes); } } if (memStream != null) { memStream.Position = 0; bitmap = new BitmapImage(); bitmap.SetSource(memStream); } return(bitmap); }
public BitmapImage(string uri) { string ext = string.Empty; byte[] bytes = null; if (uri.StartsWith("http") || uri.StartsWith("ftp")) { try { bytes = Device.Network.GetBytes(uri); } catch (Exception ex) { Device.Log.Error("Image download failed", ex); } } else if (uri.StartsWith("data:")) { bytes = ImageUtility.DecodeImageFromDataUri(uri, out ext); } else { try { bytes = Device.File.Read(uri); } catch (Exception e) { bytes = null; Device.Log.Warn("Exception reading file from :" + uri, e); } } if (string.IsNullOrEmpty(ext) && uri.ToLower().EndsWith("png")) { ext = "png"; } if (bytes == null) { return; } _buffer = bytes; Format = ext.ToLower() == "png" ? ImageFileFormat.PNG : ImageFileFormat.JPEG; }
public static void SetDrawable(string url, Action <Drawable, string> callback, ImageCreationOptions options = ImageCreationOptions.None, TimeSpan cacheDuration = default(TimeSpan)) { if (callback == null) { return; } if (string.IsNullOrEmpty(url)) { callback.Invoke(null, url); return; } #region Check memory cache var skipCache = (options & ImageCreationOptions.IgnoreCache) == ImageCreationOptions.IgnoreCache; var cached = skipCache ? null : Device.ImageCache.Get(url); if (cached is ImageData droidImage) { callback.Invoke(new BitmapDrawable(Resources, droidImage.Bitmap), url); return; } #endregion if (cacheDuration == default(TimeSpan)) { cacheDuration = Timeout.InfiniteTimeSpan; } //Check to see if another view is already waiting for this url so we don't download it again var currentDownload = PendingDownloads.GetValueOrDefault(url); if (currentDownload != null) { currentDownload.Add(callback); return; } else { PendingDownloads[url] = new List <Action <Drawable, string> > { callback }; } Device.Thread.Start(() => { var storage = (AndroidFile)Device.File; var cacheFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), url.GetHashCode().ToString(CultureInfo.InvariantCulture).Replace("-", "N") + ".urlimage"); Bitmap bitmap = null; Stream assetStream; Drawable drawable = null; var resourceId = 0; if (url.StartsWith("data:")) { // Load data URIs like files if (!storage.Exists(cacheFile)) { storage.Save(cacheFile, ImageUtility.DecodeImageFromDataUri(url, out string ext)); } } else if ((resourceId = storage.ResourceFromFileName(url)) > 0) { drawable = Resources.GetDrawable(resourceId); } else if ((assetStream = storage.GetAsset(url)) != null) { bitmap = BitmapFactory.DecodeStream(assetStream); } else if (cached != null) { var bytes = cached.GetBytes(); if (bytes != null && bytes.Length > 0) { bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length, new BitmapFactory.Options { InDensity = (int)DisplayMetricsDensity.Default, InTargetDensity = (int)((int)DisplayMetricsDensity.Default * DroidFactory.DisplayScale), }); } } else if (!storage.Exists(url) && (!storage.Exists(cacheFile) || new FileInfo(cacheFile).LastWriteTimeUtc > DateTime.UtcNow - cacheDuration)) { try { var uri = Java.Net.URI.Create(url); var bytes = string.IsNullOrEmpty(uri.Scheme) ? null : Device.Network.GetBytes(url); if (bytes != null && bytes.Length > 0) { storage.Save(cacheFile, bytes); } } catch (Exception ex) { Device.Log.Error("Image download failed", ex); } } else { bitmap = LoadFromStorage(url, 0, 0); } if (storage.Exists(cacheFile)) { bitmap = LoadFromStorage(cacheFile, 0, 0); } if (!skipCache && bitmap != null) { Device.ImageCache.Add(url, new ImageData(bitmap, url)); } var downloads = PendingDownloads[url]; PendingDownloads.Remove(url); if (drawable == null && bitmap != null) { drawable = new BitmapDrawable(Resources, bitmap); } foreach (var iv in downloads) { Device.Thread.ExecuteOnMainThread(() => { iv.Invoke(drawable, url); }); } }); }
public async static Task <BitmapImage> LoadBitmapAsync(string uri) { if (string.IsNullOrWhiteSpace(uri)) { return(null); } Stream memStream = null; if (uri.StartsWith("data:image")) { string ex; var data = ImageUtility.DecodeImageFromDataUri(uri, out ex); if (data != null) { memStream = new MemoryStream(data); } } else if (uri.StartsWith("http") || uri.StartsWith("ftp")) { memStream = new MemoryStream(); var httPeter = new HttpClient(); try { var stream = await httPeter.GetStreamAsync(uri); await stream.CopyToAsync(memStream); } catch (Exception e) { iApp.Log.Error("Image load failed: " + uri, e); memStream = null; } } else { byte[] bytes = iApp.File.Read(uri, EncryptionMode.NoEncryption); if (bytes == null) { iApp.Log.Info("Image load failed: " + uri); } else { memStream = new MemoryStream(bytes); } } if (memStream == null) { return(null); } var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.None; memStream.Position = 0; bitmap.StreamSource = memStream; try { bitmap.EndInit(); //bitmap.Freeze(); } catch (Exception e) { iApp.Log.Error(e); bitmap = null; } return(bitmap); }