简体   繁体   中英

Mono for android: Base64 string to image in gridview

I have a problem with converting some base64 string that I get from a dataset politely served by a webservice into an image and thereafter displaying it on a gridview.

My incoming data from a webservice

<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>

My Image Adapter

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

Here is where my current knowledge and understanding stops (I am a newbie on the field - did some web stuff and light javascript before but nothing compared to this). I would politely ask someone to write me up an example or at least point me to a tutorial that actually dabbles in this specific problem.

Thank you for your time.

If you take a look at http://github.com/redth/wshlst then I know he uses Base64 image conversion there.

Specifically see: https://github.com/Redth/WshLst/blob/master/WshLst.MonoForAndroid/Views/EntryView.cs and: https://github.com/Redth/WshLst/blob/master/WshLst.MonoForAndroid/NativeConveters.cs

Images are loaded from strings using code like:

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

I resolved my problem by using a listvew and custom array adapter.

As for the images:

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

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