SIGSEGV when setting ImageSource

AlexanderM 96 Reputation points
2021-03-19T12:52:41.637+00:00

I'm getting

[libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xff1b5ffc in tid 1579 (tipass.MyApp), pid 1579 (tipass.MyApp)

when trying to set Source of Image (on android).

I understand that the reason of this error usually is InvokeOnMainThread or something like that, but can not find out what exactly. I would be grateful for any suggestions

XAML:

<Image Source="{Binding GifImageSource, Converter={StaticResource byteArrayToImageSourceConverter}}" IsAnimationPlaying="True" BindingContext="{Binding Ticket}" IsVisible="{Binding IsActivated}" RotationY="{Binding GifRotationY}" Aspect="AspectFill"/>

byteArrayToImageSourceConverter - is a converter from Xamarin Community Toolkit

ViewModel:

await MainThread.InvokeOnMainThreadAsync(async () => {
  var bytes = ResourceFileToBytes("img.gif");
  GifImageSource = bytes;
});

public static byte[] ResourceFileToBytes(string r)
{
  var assembly = typeof(ByteArrayHelper).GetTypeInfo().Assembly;
  byte[] buffer = null;
  using (var stream = assembly.GetManifestResourceStream($"MyApp.Resources.{r}"))
  {
    if (stream != null)
    {
      long length = stream.Length;
      buffer = new byte[length];
      stream.Read(buffer, 0, (int)length);
    }
  }
  return buffer;
}

img.gif is placed in Resources folder, build action = embeddedResource

EDIT:

Removed converter from XAML:

XAML:

<Image Source="{Binding GifImageSource}" IsAnimationPlaying="True" BindingContext="{Binding Ticket}" IsVisible="{Binding IsActivated}" RotationY="{Binding GifRotationY}" Aspect="AspectFill"/>

ViewModel:

await MainThread.InvokeOnMainThreadAsync(async () => {
  var bytes = ByteArrayHelper.ResourceFileToBytes("img.gif");
  var stream1 = new MemoryStream(bytes);
  Ticket.GifImageSource = ImageSource.FromStream(() => stream1);
});

The result is the same:

[libc] Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xff1b5ff8 in tid 4839 (tipass.MyApp), pid 4839 (tipass.MyApp)

What am I doing wrong?

EDIT:

found my problem on xamarin's github:
[Bug] ImageSource.FromStream() causes Android to crash with animated .gif files. #10895
https://github.com/xamarin/Xamarin.Forms/issues/10895

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,324 questions
{count} votes

Accepted answer
  1. AlexanderM 96 Reputation points
    2021-03-22T09:17:01.82+00:00

    I managed to get this to work by saving files from webservice to storage first.

    viewModel:

    public ImageSource MyImageSource{ get; set; }
    ...
    if (imageLoaded) {
      string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
      string localFilename = "img2.jpg";
      string localPath = Path.Combine(documentsPath, localFilename);
      File.WriteAllBytes(localPath, bytes);
      MyImageSource = ImageSource.FromFile(localPath); // file from webservice
    } else {
      MyImageSource = "img.gif"; // default file, stored in drawable folder on android
    }
    

    XAML:

    <Image Source="{Binding MyImageSource}" IsAnimationPlaying="True" Aspect="AspectFill"/>
    
    0 comments No comments

0 additional answers

Sort by: Most helpful