【问题标题】:loopj - Android Asynchronous Http Client upload image from drawableloopj - Android 异步 Http 客户端从 drawable 上传图像
【发布时间】:2014-12-30 10:56:49
【问题描述】:

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

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

谁能帮帮我?谢谢之前

【问题讨论】:

    标签: android image asynchronous loopj


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 2017-08-30
      相关资源
      最近更新 更多