到base64的200kb图像无法发送到Web服务 [英] 200kb image to base64 cannot send to web service

查看:238
本文介绍了到base64的200kb图像无法发送到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络服务中发送base64图像时,我收到了错误的请求400, 我将base64字符串放入JSON,并使用改型将其发布.这是我的代码

I got bad request 400 when i send base64 image in the web service, I put base64 string to JSON and POST it using retrofit. here is my code

Bitmap bm = BitmapFactory.decodeFile("IMAGE PATH");
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream .toByteArray();
            String imgbase64 = Base64.encodeToString(byteArray, Base64.NO_WRAP + Base64.URL_SAFE);

并添加到JSONObject

And add to JSONObject

  JSONObject jo = new JSONObject();
            try {
                jo.put("id",users.get(counter).getId());
                jo.put("firstname",users.get(counter).getFirstname());
                jo.put("middlename",users.get(counter).getMiddlename());
                jo.put("lastname",users.get(counter).getLastname());
                jo.put("suffix",users.get(counter).getSuffix());
                jo.put("image",imgbase64);
          jo.put("date_registered",users.get(counter).getDate_registered());


            } catch (JSONException e) {
                e.printStackTrace();
            }

我将使用翻新版发送它

 ServiceInterface si = RestClient.getClient();
 Observable<Response> call = si.postUser(jo.toString());

图片大小为200kb,我收到了错误的错误请求 但是当我的图片大小只有10kb时,效果很好. 请帮忙.

Image size is 200kb and i got an error bad request but when my image size is only 10kb it works fine. please help.

推荐答案

尝试使用以下解决方案解码图像并发送

Try to use Below solution for decode the image and send

   Bitmap bitmap;
   String encodedString ="";
   String filepath=""; // your uploaded image file path

   try {
            BitmapFactory.Options options = null;
            options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filepath, options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Must compress the Image to reduce image size to make upload easy
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

            byte[] byte_arr = stream.toByteArray();
            // Encode Image to String
            encodedString = Base64.encodeToString(byte_arr, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

您可以将 encodedString 放入JSON.

You can put encodedString to the your JSON.

这是PHP Web服务,用于解码图像并将图像上传到服务器,还可以对数据库执行操作.

Here is the PHP webservice for decode and upload image to server, also perform your operation to database.

 <?php

   include('../db/config.php');  

    // Get image string posted from Android App
    $base=$_REQUEST['image'];
    // Get file name posted from Android App
    $filename = $_REQUEST['filename'];// this your image filename like testimage.jpg

    //other files
    $id= $_REQUEST['id'];
    $firstname = $_REQUEST['firstname'];
    $middlename = $_REQUEST['middlename'];// add your parameter

    // Decode Image
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
    // Images will be saved under 'www/imgupload/uplodedimages' folder
    $file = fopen('uploads/'.$filename, 'wb');
    // Create File
    fwrite($file, $binary);
    fclose($file);
    // echo 'Image upload complete, Please check your php file directory';
    if(isset($filename) && $filename !=''){
        $fileUrl = "http://yourdomain/android/upload/".$filename;
      }else{
        $fileUrl = '';
      }
    $sql=mysql_query("INSERT INTO tbl_name(Id,firstName,middleName,FileUrl) VALUES ('$Id','$firstName','$middleName','$fileUrl')");

  ?>

我建议尝试使用此解决方案,它将起作用.

I suggest to try to use this solution, It will work.

这篇关于到base64的200kb图像无法发送到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆