Xamarin窗体Android显示图片抛出异常



在页面中显示库中的多张图片会引发异常。在一页上显示一张分辨率为(例如3264x2488)的照片即可。但当它以高分辨率显示多个时,它会在安卓系统上崩溃。分辨率越高,页面上显示的内容就越少。

https://github.com/Crunch91/RezepteTagebuch/blob/master/RezepteTagebuch/RezepteTagebuch/Views/RecipeView.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="RezepteTagebuch.Views.RecipeView">
    <...>
    <StackLayout HorizontalOptions="Fill" Orientation="Horizontal">
      <Image Source="{Binding FoodPicture}"/>
      <Image Source="{Binding DescriptionPicture}"/>
    </StackLayout>
    </...>

我可以在ListView中看到同样的行为。ListView中的一张照片效果良好。一旦我添加了另一张照片,它就会再次崩溃。

https://github.com/Crunch91/RezepteTagebuch/blob/master/RezepteTagebuch/RezepteTagebuch/Views/AllRecipeView.xaml

<StackLayout>   
  <ListView ItemsSource="{Binding Recipes}" x:Name="recipeList">    
    <...>
    <ViewCell>
      <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
            <Image WidthRequest="44" HeightRequest="44" Source="{Binding FoodPicturePath}" />              
      </StackLayout>
    </ViewCell>
    </...>
  </ListView>
</StackLayout>

这是我的回购,你可以在这里找到我正在运行的Xamarin Forms应用程序:

https://github.com/Crunch91/RezepteTagebuch

我正在三星Galaxy S3上调试安卓4.3

如果有人能帮忙,我将不胜感激。

更新:

@idoT是对的。我收到一个"内存不足异常"。

在显示图像之前,调整图像大小的最佳方法是什么?

有一篇关于Android开发者如何高效加载大型位图的好文章:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

理想情况下,你应该为屏幕上没有显示的每一张图片释放内存,以释放资源。

最新更新