当我上传多个图片时,我的代码正在为一张图片工作,它向我显示错误。
有人可以帮我解决这个问题吗?
使用下面的代码,我可以上传单张图片,但我想上传多张图片,所以请帮助我如何上传图片。 感谢
这是我的代码
String image_path=imageUrl1;
GetData(myFiles);
}
});
}
public void GetData(String myFiles)
{
String image_retrive_url="http://tradewale.com/open/tradedata.php?submitType=PostAd";
pDialog = new ProgressDialog(PostAddActivity.this);
pDialog.setMessage(" Sending...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
Bitmap bitmap = BitmapFactory.decodeFile(myFiles);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
String Title = title.getText().toString();
String Description = description.getText().toString().trim();
String PhoneNumber = phonenumber.getText().toString();
String Email = email.getText().toString();
String Price = price.getText().toString();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair("photo_url", image_retrive_url));
Thread t = new Thread(new Runnable()
{
@Override
public void run() {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://tradewale.com/open/tradedata.php?submitType=PostAd");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String the_string_response = convertResponseToString(response);
}
catch(final Exception e)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Toast.makeText(PostAddActivity.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
System.out.println("Error in http connection "+e.toString());
}
}
});
t.start();
}
答案 0 :(得分:0)
在webservice中发送更多图像。您还必须更改webservice
。在服务部分添加one more
参数。这样你就可以一次发送多张图像。
并获取base64
e.g. img_str2
字符串
喜欢:
String image_str = Base64.encodeBytes(byte_arr);
String image_str2 = Base64.encodeBytes(byte_arr2);
并在服务中命名新参数,例如image2
nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair("image2",image_str2));
此处image
和image2
为2 diff
。图像参数。这些参数名称也必须是define in your service side code
..这样服务代码可以保存您的iamges。