I'm trying to send an image from drawable to rest web service use android asynchronous http client. This link show how to send image use path,
Is it possible to send image from drawable? I have already look from Android Asynchronous Http Client about how to send byte image, but in my code it's keep return error that the picture is missing. Here is my code
RequestParams params = new RequestParams();
username = editTextEmail.getText().toString();
name = editTextName.getText().toString();
password = editTextPassword.getText().toString();
phone = editTextPhone.getText().toString();
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_user);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte = stream.toByteArray();
if(isOnline()){
params.put("email", username);
params.put("name", name);
params.put("password", password);
params.put("phone", phone);
params.put("picture", new ByteArrayInputStream(imageInByte), "user.jpg");
invokeWS(params);
return true;
}
Can anyone help me? Thanks before
Yes, it's possible to send image from drawable, but you should convert your image to base64 string like this :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
and then send the string to the server :
params.put("image", image_str);
For more details look here Android application to send image to MySQL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.