如何使用webservices和json在android xamarin中将图像从sql数据库显示到listview?
如何使用webservices和json在android xamarin中将图像从sql数据库显示到listview如何使用webservices和json在android xamarin中将图像从sql数据库显示到listview?,android,asp.net,xamarin,Android,Asp.net,Xamarin,如何使用webservices和json在android xamarin中将图像从sql数据库显示到listview public override View GetView(int position, View convertView, ViewGroup parent) { var item = objList[position]; if (convertView == null) { convertView
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = objList[position];
if (convertView == null)
{
convertView = objActivity.LayoutInflater.Inflate(Resource.Layout.ContListViewHospName, null);
}
convertView.FindViewById<TextView>(Resource.Id.tvHospID).Text = item.HospID;
convertView.FindViewById<TextView>(Resource.Id.tvHospName).Text = item.HospName;
byte[] img = (byte[])item.HospLogo;
Bitmap bitmap = BitmapFactory.DecodeByteArray(item.HospLogo, 0, item.HospLogo.Length);
convertView.FindViewById<ImageView>(Resource.Id.imgLogo).SetImageBitmap(bitmap);
return convertView;
}
public override View GetView(int位置、视图转换视图、视图组父视图)
{
var项目=对象列表[位置];
if(convertView==null)
{
convertView=objActivity.LayoutInflater.Inflate(Resource.Layout.ContListViewHospName,null);
}
convertView.findviewbyd(Resource.Id.tvospid).Text=item.HospID;
convertView.FindViewById(Resource.Id.tvHospName).Text=item.HospName;
字节[]img=(字节[])item.HospLogo;
位图位图=位图工厂.DecodeByteArray(item.HospLogo,0,item.HospLogo.Length);
convertView.findviewbyd(Resource.Id.imgLogo).SetImageBitmap(位图);
返回视图;
}
可能是您的图像太大,请尝试缩放它,如
还要确保在布局中设置了适当的尺寸,如宽度和高度
我将二进制值存储在一个字符串变量中,然后使用json将值传输到android项目中的cs文件中
您在一个字符串中存储的二进制值应该如下所示:“ivborw0kggoaaaansuhueugaaaaaaaaaaiacayaaaad0ent6aaabhncsvqicagifahkiaaaaaaaaiabajrefuejzsvfd33eiw7/mJgEtHb0WKFGWqSuW96epqM9P9+s30mZ/2j33n7O7Zt/Nme8zr6emu6i5fk……。”
这是64个字节。您不能仅使用(byte[])
将字符串
转换为byte[]
:
您需要使用System.Convert.FromBase64String()
函数
//The string is download form server in the json file
string myBytes = "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzsvfd33EiW7/mJgEtHb0WKFGWqS........"
byte[] decByte3 = System.Convert.FromBase64String(myBytes);
然后将字节转换为图像:
public Bitmap Bytes2Bimap(byte[] b)
{
if (b.Length != 0)
{
return BitmapFactory.DecodeByteArray(b, 0, b.Length);
}
else
{
return null;
}
}
并将位图转换为可绘制:
Bitmap myIcon = Bytes2Bimap(decByte3);
Drawable myIconD = new BitmapDrawable(myIcon);
imageView1.Background = myIconD;
我在数据库中的图像设置为100px 100px png格式,问题是当我通过sql server数据库从数据库中获取图像时,我将二进制值存储在一个字符串变量中,然后使用json将值传输到android项目中的cs文件中。现在在cs文件中,当我获取图像的值时,它以System.Byte[]格式显示。现在,若我将其存储在Byte[]变量中,则仅当我使用类型转换时,它才允许我。现在,当我试图将这个字节[]转换为位图以设置为imageView时,它给出了一个错误。这是我的主要问题。提前谢谢。非常感谢你。。这对我很有用。。经过三天的研究,这段代码给了我一个结果。。谢谢。