我正在写它,我正在开发一个应用程序,用户通过图像点击图片,图像将存储在SD卡中,然后我从图像视图中选择图片并将该图片发送到服务器。我已将我的图像转换为字节数组,然后以Base64字符串格式对其进行编码,但是当我尝试将该图像发送到服务器时,它会给我一个soap错误的错误。这是我的Android设备代码:
public void doneImage(){
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, out);
byte[] imagebyte = out.toByteArray();
String jp = caption.getText().toString();//for fetching the path of selected image path.//
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
String strBase64 = Base64.encode(imagebyte);
PropertyInfo addProp = new PropertyInfo();
addProp.setName("imgdata");
addProp.setValue(strBase64);
addProp.setType(String.class);
Request.addProperty(addProp);
PropertyInfo addProp1 = new PropertyInfo();
addProp1.setName("FileName");
addProp1.setValue(jp);
addProp1.setType(String.class);
Request.addProperty(addProp1);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet=true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE aht=new HttpTransportSE(URL);
try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse();
Toast.makeText(getApplicationContext(), "Success" +response, Toast.LENGTH_LONG).show();
}
catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
dialog.cancel();
Toast.makeText(getApplicationContext(),
e.toString(),
Toast.LENGTH_LONG).show();
caption.setText(e.toString());
//return null;
//Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = data.getData();
String filePath = null;
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
filePath = selectedImagePath;
caption.setText(selectedImagePath);
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
caption.setText(filemanagerstring);
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
} else {
bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
break;
default:
}
}
在服务器端,我使用了vb.net Web服务。代码是:
_ 公共函数UploadImage(ByVal imgdata As String,ByVal FileName As String)As Integer
'Find the path on the server of our apps images folder
Dim FilePath As String = Server.MapPath("images")
'strip the path and .jpg etc out of our filename
Dim i As Integer = InStrRev(FileName, "\")
FileName = Mid(FileName, i)
Dim j As Integer = InStr(FileName, ".") - 1
Dim k As Integer = Len(FileName)
FileName = Mid(FileName, 1, j)
'Storing an image to bitmap after converting from base64 format'
Dim Bm As New System.Drawing.Bitmap(Base64ToImage(imgdata))
'Convert the bitmap to a png and save it in our images folder
BM.Save(FilePath & FileName, Imaging.ImageFormat.Jpeg)
'eventually we will create a database entry for this image and return the image ID
Return 1
End Function
'For converting the base64 satring value into the image'
Public Function Base64ToImage(ByVal base64String As String) As Image
' Convert Base64 String to byte[]
Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
' Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length)
Dim image__1 As Image = Image.FromStream(ms, True)
Return image__1
End Function
请大家帮助我,我在过去的4天里一直在努力。 任何帮助都会得到赞赏。并提前致谢......
答案 0 :(得分:1)
休息会更好地从android调用服务。 无论如何,如果您想以您的方式执行此操作,您可以将流发送到您的服务,制作一个需要流的方法。 并且你的android端询问输出流的连接并将所有字节写入它。
我用Restful wcf完成了这个。 以供参考 : Uploading MS Word files from Android to .Net WCF?