简体   繁体   中英

Xamarin Image Source Not Showing on Android

I have a cross platform app using a PCL where you can take a photo using the camera, the image is saved to a local db (to later be synced elsewhere). The app also loads the last image and displays it on screen in an with the source bound to the view model.

This works perfectly on iOS but Android wont show the bound image.

I have tested that i can show an image on android by setting the Source to "icon.png" which works.

Xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>            
    <Image HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFit" BackgroundColor="Green" Source="{Binding CameraImage}"/>
    <Button Grid.Row="1" Text="Take Picture" Command="{Binding GetPhoto}"/>
</Grid>

ViewModel

public void LoadLatestImage(object sender, EventArgs e)
{
    var imageDBO = DatabaseHelpers.LocalDatabase.Table<ImageDBO>().OrderByDescending(i => i.DateCreated).FirstOrDefaultAsync().Result;
    if (imageDBO != null)
    {
        CameraImage = ImageSource.FromStream(() => { return new MemoryStream(imageDBO.ImageData); });
    }
}

Binding

private ImageSource _cameraImage;
public ImageSource CameraImage
{
    get { return _cameraImage; }
    set
    {
        if (_cameraImage != value)
        {
            _cameraImage = value;
            OnPropertyChanged(nameof(CameraImage));
        }
    }
}

Just a note: I have attempted to find a solution to this on Xamarin forums and stack overflow but similar issues on here aren't the same and their solutions don't seem to help. It seems most of these issues are caused by access to local resources however that shouldn't apply here as my source is straight from a bite array

Turns out this was happening due to the Byte array produced by the image from the Android camera being too large to bind to the view (an android issue only). Reducing the image size/quality fixed the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM