【问题标题】:Mono for android: Base64 string to image in gridviewMono for android:Base64 字符串到 gridview 中的图像
【发布时间】:2012-12-13 12:29:02
【问题描述】:

我在将一些 base64 字符串转换为图像时遇到问题,这些字符串是我从 web 服务礼貌地提供的数据集中获得的,然后将其显示在 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,但与此相比没有什么)。我会礼貌地请别人给我写一个例子,或者至少给我指出一个真正涉足这个特定问题的教程。

感谢您的宝贵时间。

【问题讨论】:

  • 我忘了提到我使用 Visual Studio 2010 和最新版本的 Mono for Android。
  • 撞车?我真的需要这些人的帮助。

标签: c# mono xamarin.android xamarin


【解决方案1】:

如果你看一下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);

【讨论】:

    【解决方案2】:

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

    至于图片:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多