简体   繁体   English

如何在Android中将图像上传到服务器

[英]how to upload an image to server in android

I am using params.put to post the strings to the server . 我正在使用params.putstringsserver But how do I post an image which is saved in the variable imgPreview . 但是如何发布保存在imgPreview变量中的imgPreview

comp_logo_id is the image field in Rest Api. comp_logo_id是Rest Api中的图像字段。

Here is My code: 这是我的代码:

            params.put("title", title);
            params.put("comp_logo_id", comp_logo_id);
            params.put("company_name", company_name);
            params.put("industry_selected", industry_selected);

You must convert image to base64 and upload to server . 您必须将映像转换为base64并上传到server。 In server convert base64 to image . 在服务器中,将base64转换为image。

  public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 70, stream);
        byte[] byteFormat = stream.toByteArray();
        // get the base 64 string
        String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);

        return imgString;
   }
   String encodedImageData =getEncoded64ImageStringFromBitmap(your bitmap);
   params.put("comp_logo_id", encodedImageData );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM