Mono for android:Base64字符串到gridview中的图像

时间:2012-12-28 07:26:38

标签: c# mono xamarin.android xamarin

我将从网络服务礼貌服务的数据集中获得的一些base64字符串转换为图像,然后在gridview上显示它时出现问题。

来自网络服务的传入数据

<NewDataSet xmlns="">
    <AvailableUsers diffgr:id="AvailableUsers1" msdata:rowOrder="0">
        <sUserId>1</sUserId>
        <UserDesc>Mr. Someone</UserDesc>
    </AvailableUsers>
    <AvailableUsers diffgr:id="AvailableUsers2" msdata:rowOrder="1" diffgr:hasChanges="modified">
        <sUserId>2</sUserId>
        <UserDesc>Mr. Someone 2</UserDesc>
        <UserIMG>
        // A base64 string is here but let's not bother ourselves with that here...
        </UserIMG>
    </AvailableUsers>
</NewDataSet>

我的图片适配器

class ImageAdapter : BaseAdapter
{
    Context context;

    public ImageAdapter(Context c)
    {
        context = c;
    }

    public override int Count
    {
        get { return thumbIds.Length; }
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;

        if (convertView == null)
        {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.LayoutParameters = new GridView.LayoutParams(150, 150);
            imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
            imageView.SetBackgroundColor(Color.Aqua);
        }
        else
        {
            imageView = (ImageView)convertView;
        }

        imageView.SetImageResource(thumbIds[position]);
        return imageView;
    }

    int[] thumbIds = {
    Resource.Drawable.sample_0, Resource.Drawable.sample_1, Resource.Drawable.sample_2, Resource.Drawable.sample_3
};
}

这是我目前的知识和理解停止的地方(我是该领域的新手 - 之前做了一些网络内容和轻量级的javascript,但没有比这更好)。我会礼貌地请某人给我写一个例子,或者至少指出一个实际涉及这个特定问题的教程。

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

如果您查看http://github.com/redth/wshlst,我知道他在那里使用了Base64图像转换。

具体请参阅:https://github.com/Redth/WshLst/blob/master/WshLst.MonoForAndroid/Views/EntryView.cs和:https://github.com/Redth/WshLst/blob/master/WshLst.MonoForAndroid/NativeConveters.cs

使用以下代码从字符串加载图像:

var bytes = System.Convert.FromBase64String(base64);
var drawable = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);

答案 1 :(得分:0)

我通过使用listview和自定义数组适配器解决了我的问题。

至于图像:

string base64 = item.UserIMG;

if (item.UserIMG != null) // If there's actually a string inside item.UserIMG
{
    System.IO.Stream s = new MemoryStream(Convert.FromBase64String(base64));

    byte[] arr = Convert.FromBase64String(base64);
    Drawable img = Drawable.CreateFromStream(s, null);

    ImageView UserAvatar = view.FindViewById<ImageView>(Resource.Id.imgView);
    UserAvatar.SetImageDrawable(img);
 }
 else  // If item.UserIMG is "" or null
    {
       ImageView UserAvatar = view.FindViewById<ImageView>(Resource.Id.imgView);
    }