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