[英]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. 我将我从网络服务礼貌地提供的数据集中得到的一些base64字符串转换为图像,然后将其显示在gridview上时遇到问题。
My incoming data from a webservice 我从Web服务传入的数据
<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). 这是我目前的知识和理解停止的地方(我是该领域的新手-之前做了一些Web东西和简单的javascript,但与此相比没有任何东西)。 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. 如果您看一下http://github.com/redth/wshlst,那么我知道他在那里使用了Base64图像转换。
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 具体请参见: https : //github.com/Redth/WshLst/blob/master/WshLst.MonoForAndroid/Views/EntryView.cs和: 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. 我通过使用listvew和自定义阵列适配器解决了我的问题。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.