200kb image to base64 cannot send to web service

喜欢而已 提交于 2019-12-25 04:13:40

问题


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

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

And I will send it using retrofit

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

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


回答1:


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

You can put encodedString to the your JSON.

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.



来源:https://stackoverflow.com/questions/40459388/200kb-image-to-base64-cannot-send-to-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!