无法在Xamarin.Form android中显示byte []图像

时间:2018-01-06 21:50:35

标签: c# xamarin.forms

我正在尝试在从字节[]转换的列表视图中显示图像,但我一直收到此错误:

  

01-06 15:18:13.863 D / skia(3589):--- SkImageDecoder :: Factory   返回null [0:] ImageLoaderSourceHandler:图像数据无效:   Xamarin.Forms.StreamImageSource

我已经搜索了这个错误消息,并在堆栈溢出时找到了很多答案,但没有任何一个有帮助。下面是我显示图像的代码。

查看:

public void Notification(final String mytext) { 
final Intent notifIntent = new Intent("action_call_method");
PendingIntent pendingmyIntent = PendingIntent.getBroadcast(context, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);

BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
              if (intent.getAction().equals("action_call_method")) {
                My_Function(mytext);
            } 
        }
    };

IntentFilter filter = new IntentFilter("action_call_method");
context.registerReceiver(receiver, filter);

        Notification noti = new Notification.Builder(context)
         .setTicker(ticker)
         .setContentTitle(title)
         .setContentText(text)
         .setSmallIcon(R.drawable.ic_lock_silent_mode_off) 
         .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
         .setAutoCancel(true)
         .setContentIntent(pendingmyIntent)
         .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
    }

当我使用 <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0"> <!--<Image Source="groceries.png"/>--> <Image x:Name="MatePhoto" Source="{Binding Photo, Converter={StaticResource ByteToImageConverter},Mode=Default}"/> </StackLayout> 时,它可以工作,但绑定不起作用。

转换器:

<Image Source="groceries.png"/>

现在假设我有这个字节字符串,我将其转换为Byte []并且我的转换器将byte []转换为image并绑定到视图。

 public class ByteToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || value is DBNull)
            return null;
        var bArray = (byte[])value;

        var imgsrc = ImageSource.FromStream(() => {
            var ms = new MemoryStream(bArray);
            ms.Position = 0;
            return ms;
        });

        return imgsrc;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果您需要了解更多信息,请与我们联系,我们将不胜感激。感谢

0 个答案:

没有答案