2015-06-20 43 views
1

在頁面中顯示來自圖庫的多張圖片會引發異常。在一個頁面上以分辨率(例如3264x2488)顯示一張照片。但是,當涉及到顯示多個高分辨率時,它會在Android上崩潰。分辨率越高,可以在頁面上顯示的越少。Xamarin表單Android顯示圖片拋出異常

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窗體應用程序:

https://github.com/Crunch91/RezepteTagebuch

我調試在三星Galaxy S3搭載Android 4.3

如果有人能幫忙,我將不勝感激。

UPDATE:

@idoT是正確的。我得到一個「OutOfMemoryException」。

在顯示圖像之前調整圖像大小的最佳方法是什麼?

+0

你可以嘗試降低這些圖像的分辨率嗎?我的猜測是你得到了一個內存不足的例外 – IdoT

+0

是的,那是真的。這是一個內存不足的例外。我嘗試使用較低分辨率400x400的照片。現在我可以顯示更多圖像。但是當顯示超過20張圖像時,它仍然會崩潰。 – Crunch

+0

您可能需要進一步減少它們,當圖像未顯示在頁面中時,回收機制應該開始回收。在一個頁面中可以放入多少張圖像,以及您有多少可用內存? – IdoT

回答

2

有關於Android開發者一個偉大的文章如何load large bitmaps efficiently

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); 
} 

理想情況下,你會釋放內存爲未顯示在屏幕上以釋放資源每一個畫面。

+0

我想這可以用於一個只有android的項目。但我的麻煩是針對xamarin表格項目。 – Crunch