0

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,

Uploading an image from Android (with Android Asynchronous Http Client) to rails server (with paperclip)

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

Community
  • 1
  • 1
mrconga
  • 31
  • 7

1 Answers1

1

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

Community
  • 1
  • 1
Josef
  • 442
  • 7
  • 16