question

AlexanderM-2799 avatar image
0 Votes"
AlexanderM-2799 asked ·

SIGSEGV when setting ImageSource

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





dotnet-xamarinformsdotnet-xamarinforms-xaml
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It seems you have solved the problem : https://github.com/xamarin/Xamarin.Forms/issues/10895#issuecomment-803067320.
Would you mind sharing your solution below and mark it as answer ?

0 Votes 0 ·

1 Answer

AlexanderM-2799 avatar image
0 Votes"
AlexanderM-2799 answered ·

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"/>


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.