1

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

Geoff
  • 6,277
  • 23
  • 87
  • 197
  • nitpick: base64 isn't encryption. it's an ENCODING. – Marc B Sep 14 '16 at 16:45
  • Okay but the file is size and original output is changed – Geoff Sep 14 '16 at 16:47
  • plus your database code is basically totally broken. You do **NOT** get an id by pulling all the existing ids and throwing away all but the last one. plus, the $id you DO end up with will NOT be the id of the new record you created when you insert that url, so you're killing the id/url association by making everythin be off-by-one. – Marc B Sep 14 '16 at 16:47
  • The original file has an encryption – Geoff Sep 14 '16 at 16:47
  • The main issue is not on the database but on sending the file to php in an original format – Geoff Sep 14 '16 at 16:48
  • base64 is a 1:1 encoding. what you put into it will be what you get out of it. if something's changing the size of your data, then it's not b64's fault. – Marc B Sep 14 '16 at 16:49
  • is there another way of uploading the files without use of encoding way – Geoff Sep 14 '16 at 16:51
  • Possible duplicate of [Upload an image using Google Volley](http://stackoverflow.com/questions/29430599/upload-an-image-using-google-volley) – JimmyB Sep 14 '16 at 20:54
  • No its not, I have updated the question, i would like to upload without compressing the image – Geoff Sep 15 '16 at 05:08

1 Answers1

0

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.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68