I'm trying to upload image using this code:
我正在尝试用这段代码上传图片:
private String uploadFile() {
String responseString = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL_M);
Log.i("UploadApp", "upload url: " + Config.FILE_UPLOAD_URL_M);
AndroidMultiPartEntity entity;
entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
// publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile;
sourceFile = new File(compressImage(filePath));
Log.i("UploadApp", "file path: " + filePath);
// Adding file data to http body
entity.addPart("f", new FileBody(sourceFile)); //problem is here
entity.addPart("category", new StringBody("Bill"));
entity.addPart("description", new StringBody("test single"));
entity.addPart("file", new StringBody("unknown1"));
entity.addPart("clientid", new StringBody("4"));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: " + statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
Log.e("UploadApp", "exception: " + responseString);
} catch (IOException e) {
responseString = e.toString();
Log.e("UploadApp", "exception: " + responseString);
}
return responseString;
}
I have a web service for uploading image which has 5 parameters and 4 of them are string but one is byte array (byte[] f)
我有一个用于上传图像的web服务,它有5个参数,其中4个是字符串,但一个是字节数组(byte[] f)
main problem: how to convert my source file(image) in byte array to upload the image on server in above code corresponding this web service.
主要问题:如何将我的源文件(图像)转换成字节数组,并在上面对应此web服务的代码中上载到服务器上。
2 个解决方案
#1
1
You can use android third party "AndroidAsync".You can do any things from this code.plz check this
您可以使用android第三方“AndroidAsync”。您可以从这段代码中做任何事情。请检查这个
https://github.com/koush/AndroidAsync
https://github.com/koush/AndroidAsync
Thanks
谢谢
#2
0
first of all ,from source file you can get absolute path then called upload method
首先,从源文件中可以得到绝对路径,然后称为上传方法
String mCurrentPhotoPath = sourceFile.getAbsolutePath();
private String upload() {
Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, bao);
byte[] ba = bao.toByteArray();
return Base64.encodeToString(ba, Base64.DEFAULT);
}
#1
1
You can use android third party "AndroidAsync".You can do any things from this code.plz check this
您可以使用android第三方“AndroidAsync”。您可以从这段代码中做任何事情。请检查这个
https://github.com/koush/AndroidAsync
https://github.com/koush/AndroidAsync
Thanks
谢谢
#2
0
first of all ,from source file you can get absolute path then called upload method
首先,从源文件中可以得到绝对路径,然后称为上传方法
String mCurrentPhotoPath = sourceFile.getAbsolutePath();
private String upload() {
Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, bao);
byte[] ba = bao.toByteArray();
return Base64.encodeToString(ba, Base64.DEFAULT);
}