loopj - Android异步Http客户端从drawable上传图像

时间:2014-12-30 10:56:49

标签: android image asynchronous loopj

我正在尝试从drawable发送图像到休息Web服务使用android异步http客户端。此链接显示如何发送图像使用路径,

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

是否可以从drawable发送图像?我已经从Android Asynchronous Http Client了解了如何发送字节图像,但在我的代码中,它保留了图片丢失的返回错误。这是我的代码

            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;
            }

任何人都可以帮助我吗?先谢谢

1 个答案:

答案 0 :(得分:1)

是的,它可以从drawable发送图像,但您应该将图像转换为base64字符串,如下所示:

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);

然后将字符串发送到服务器:

params.put("image", image_str);

有关详细信息,请查看Android application to send image to MySQL