Xamarin forms: ImageLoaderSourceHandler: Could not retrieve image or image data was invalid:

半城伤御伤魂 提交于 2020-06-29 04:28:38

问题


Getting below error message when trying to show an image on UI.

Exception details:

[0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: "imageurl"
[0:] Image Loading: Error getting stream for "imageurl": System.ObjectDisposedException: Can not access a closed Stream.
  at System.IO.StreamHelpers.ValidateCopyToArgs (System.IO.Stream source, System.IO.Stream destination, System.Int32 bufferSize) [0x0003a] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at System.IO.MemoryStream.CopyToAsync (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at Xamarin.Forms.UriImageSource.GetStreamAsyncUnchecked (System.String key, System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x00310] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:185 
  at Xamarin.Forms.UriImageSource.GetStreamFromCacheAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x00140] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:209 
  at Xamarin.Forms.UriImageSource.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000f0] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:139 
  at Xamarin.Forms.UriImageSource.GetStreamAsync (System.Threading.CancellationToken userToken) [0x00056] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:78 
Debugger Connection Lost: Debugger lost connection to the running application. Likely this means the application terminated unexpectedly.

Version Details

Xamarin forms Version: 4.2.0.848062
Target Framework: Android 9.0 Pie
Target Android Version: Android 9.0(API Level 28 - Pie)

I am using below code for showing image on UI:

image.Source = ImageSource.FromUri(new Uri(ImageUrl));

The issue is only with the android project. Go through some similar issue threads, but none of them helps me.


回答1:


Load image from remote url is not very friendly on Android . So I suggest that you ca use the plugin FFImageLoading . FFImageLoading for Forms provides CachedImage - a direct Image class replacement. It’s used just the same but it has some additional properties.

Firstly,Add the following NuGet packages to every project(iOS and Android) in your solution.

Xamarin.FFImageLoading

Xamarin.FFImageLoading.Forms,

Xamarin.FFImageLoading.Transformations (if you’ll use transformations like CircleTransformation)

Xamarin.FFImageLoading.Svg (for SVG support)

Xamarin.FFImageLoading.Svg.Forms (for SVG support)

SkiaSharp (for SVG support)

Then init the renderer in sepecfic platforms before you init the forms.

in iOS -> AppDelegate.cs

FFImageLoading.Forms.Platform.CachedImageRenderer.Init();  

in Android ->MainActivity.cs

FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: true);

in xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="FFImageLoading.Forms.Sample.Pages.XamlSimpleExamplePage"
    xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
    Title="Basic XAML Example">
    <ContentPage.Content>
        <ffimageloading:CachedImage HorizontalOptions="Center" VerticalOptions="Center"
            WidthRequest="300" HeightRequest="300"
            DownsampleToViewSize="true"
            Source = "{Binding xxx}">
        </ffimageloading:CachedImage>
    </ContentPage.Content>
</PFContentPage>

And here is a similar case that you can check ImageCell not displaying image only on Android .



来源:https://stackoverflow.com/questions/58299753/xamarin-forms-imageloadersourcehandler-could-not-retrieve-image-or-image-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!