简体   繁体   中英

Upload an image to server in Android without encoding to Base64 using Volley

I have an uploader in android which uploads an image file(which is encrypted by an algorithim to 400bytes) but the file has to be decoded to base64 string to be saved to a mysql db as a string and upload the image to a folder.

The problem is that after decoding the file to base64 string it looses its byte encrypted algorithm format.(The image size reduces from 400 bytes to less)

This is what i have done:

Bitmap b = this.toGrayscale(mRegisterImage);  //this image file is 400byte encrypted
uploadImage(b);

This is the function uploadimage

public String getStringImage(Bitmap bmp) {  //This is what converts the image to 64encoded format string
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);  //Thos is what compresses image hence loosing bytes
    byte[] imageBytes = baos.toByteArray();

    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

private void uploadImage(final Bitmap bmp) {
    // Showing the progress dialog
    try {
        final ProgressDialog loading = ProgressDialog.show(this,
                "Uploading fingerprint...", "Please wait...", false, false);
        StringRequest stringRequest = new StringRequest(
                Request.Method.POST, Config.url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        // Disimissing the progress dialog
                            debugMessage("Return Message: " + s);
                            loading.dismiss();
                                                }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        try { // Dismissing the progress dialog
                            loading.dismiss();
                            // Showing toast
                            debugMessage(volleyError.getMessage());
                        } catch (Exception r) {
                            debugMessage("onerrorresponse: "+volleyError.getMessage());
                        }
                    }
                }) {
            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                // Converting Bitmap to String
                String image = getStringImage(bmp);

                // Creating parameters
                Map<String, String> params = new Hashtable<String, String>();

                // Adding parameters
                params.put("image", image);

                return params;
            }
        };
        // Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        // Adding request to the queue
        requestQueue.add(stringRequest);
    } catch (Exception r) {
        debugMessage("out: "+r.getMessage());
    }
}

This is the PHP CODE:

  $sql ="SELECT id FROM fingerprint ORDER BY id ASC";

  $res = mysqli_query($con,$sql);

  $id = 0;

  while($row = mysqli_fetch_array($res)){
      $id = $row['id'];
  }

 $path = "$id.png";

 $actualpath = "http://simplifiedcoding.16mb.com/PhotoUploadWithText/$path";

 $sql = "INSERT INTO fingerprint (value) VALUES ('$actualpath')";

 if(mysqli_query($con,$sql)){

     file_put_contents($path,base64_decode($image));

     echo "Successfully Uploaded";
   }

Everything works quite fine but is there a way that i can upload the actual image without converting it to a string in android using volley library. The above code compresses the image to the string, How do i change this compression so that the bytes arent lost

Basically no, because Http message body can carry byte data only so can't send any other form of this large data through http protocol.

Http transfer data by using TCP which use commonly switching techniques in this case. Protocols either format data in bit or byte level where byte level is mostly used so if you looking for another way then you are out of luck today

So even if you are using StringRequest and passing data as string parameter eventually your every parameter is converted to byte array type so you can't escape from converting data to bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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