vb.net 如何通过soap网络服务(XML)传递图像的字节数组并在android设备上解码此字节数组以将其转换回图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30119642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image
提问by Dropkick54
I want to send images over a web service to an android device. This web service is written in VB.net and it is being called by android device to fetch the data. I can fetch the data normally, but the issue is with Images. I have converted image into byte array, converted it into string and passed it over web service(XML) to android device. I cannot decode this String byte array back to an image on android side. How to achieve this? Any simple solution? Am I using wrong approach?
我想通过网络服务将图像发送到 android 设备。该 Web 服务是用 VB.net 编写的,它被 android 设备调用以获取数据。我可以正常获取数据,但问题出在图像上。我已将图像转换为字节数组,将其转换为字符串并通过网络服务(XML)将其传递给 android 设备。我无法将此字符串字节数组解码回 android 端的图像。如何实现这一目标?有什么简单的解决办法吗?我使用了错误的方法吗?
回答by Sudarshan Vidhate
For Converting Image To String :
将图像转换为字符串:
String imageAsString=Base64.encodeToString(byteArray, Base64.DEFAULT);
For Converting String To Image:
将字符串转换为图像:
byte[] imageAsBytes = Base64.decode(imageAsString.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)findViewById(R.id.ImageView);
Bitmap imgBitmap=BitmapFactory.decodeByteArray(imageAsBytes,0,imageAsBytes.length);
image.setImageBitmap(imgBitmap);
回答by Peanut
Converting a byte array to a String is a bit tricky. Since the image data may contain null bytes, the String cannot. You probably need to encode the byte array so that the String is valid. Try to use base64-encoding.
将字节数组转换为字符串有点棘手。由于图像数据可能包含空字节,因此字符串不能。您可能需要对字节数组进行编码,以便字符串有效。尝试使用 base64 编码。
Consider the following code example:
考虑以下代码示例:
System.Convert.ToBase64String(byteArray)
You need to decode the base64-encoded String in android as well. Here is an example for that as well:
您还需要在 android 中解码 base64 编码的字符串。这也是一个例子:
import org.apache.commons.codec.binary.Base64;
String base64String = "... your base64 String ...";
byte[] decodedBytes = Base64.decodeBase64(base64String.getBytes());
decodedByteswill contain your original data.
decodedBytes将包含您的原始数据。
回答by Dropkick54
We got the solution
我们得到了解决方案
On webservice side we used:
在网络服务方面,我们使用了:
System.Convert.ToBase64String(byte_arr)
On Android Side we used:
在 Android 端,我们使用了:
byte[] decodedBytes = Base64.decode(imageByteArray[i].getBytes(),Base64.DEFAULT);
Bitmap bMap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
Thank you for help. Cheers!!!
谢谢你的帮助。干杯!!!
回答by gathila
You can use org.castor.util.Base64Encoder to encode byte array to String
您可以使用 org.castor.util.Base64Encoder 将字节数组编码为字符串
byte [] doc = {37, 80, 68, 70, 45, 49, 46};
String image = new String(Base64Encoder.encode(doc));
In your web service class, use this
在您的 Web 服务类中,使用此
byte[] imageData = Base64Decoder.decode(image);

