gpt4 book ai didi

android - 服务器上传图片报错如何解决?

转载 作者:行者123 更新时间:2023-11-30 04:14:40 26 4
gpt4 key购买 nike

我又写了一遍,我正在开发一个应用程序,用户通过图像单击图片,图像将存储在 sdcard 中,然后我从 ImageView 中选择图片并将该图片发送到服务器。我已将我的图像转换为字节数组,然后以 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 网络服务。代码是:

_ Public Function 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 天里一直在努力。感谢任何帮助。提前致谢......

最佳答案

Rest 在从 android 调用服务时会更好。无论如何,如果您想按照自己的方式执行此操作,则可以将流发送到您的服务,制作一种采用流的方法。并且你的 android 端请求连接输出流并将所有字节写入它。

我已经用 Restful wcf 完成了这个。以供引用 : Uploading MS Word files from Android to .Net WCF?

关于android - 服务器上传图片报错如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10223251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com