1

I'm having issues when trying to send a Base64 encoded bitmap through a HttpURLConnection to a POST variable on my PHP script.

It displays the toast "Upload Successful" but my database is not populating.

I first convert my Bitmap variable to Base64, where bSignature is the Bitmap and sSignature is the String.

sSignature = encodeToBase64(bSignature, Bitmap.CompressFormat.JPEG,100);

Here is my AsyncTask code:

class BackgroundTaskSig extends AsyncTask<String,Void,String> {
    String json_signature_url;

    @Override
    protected void onPreExecute() {
        json_signature_url = "http://abhandyman.x10host.com/upload_signature.php";
    }

    @Override
    protected String doInBackground(String... args) {
        sSignature = args[0];
        try {
            URL url = new URL(json_signature_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data_string = URLEncoder.encode("image","UTF-8")+"="+URLEncoder.encode(sSignature,"UTF-8");
            bufferedWriter.write(data_string);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            inputStream.close();
            httpURLConnection.disconnect();
            return "Upload Successful";

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
    }
}

Here is my PHP script, where the structure signature column is set to 'blob'

<?php
require "init_signature.php";

$job_id = "1";
$image = $_POST['image'];
$image = base64_decode($image);

$sql_query = "UPDATE signature SET signature='$image' WHERE id='$job_id'";

if (mysqli_query($con,$sql_query)){
    echo "Image uploaded successfully";
}else{
    echo "Error in insertion " . mysqli_error($con);
}

?>

I have a feeling its something in my PHP script that's incorrect. Any second opinion would be much appreciated!

Thanks in advance


EDIT: So i've resolved the T_VARIABLE error message (Small syntax problem) - now i've got this error message:

php_error

Jason Fung
  • 69
  • 10
  • Unless you're auto-committing somewhere else there's no commit on the insert. – Mike Summers Sep 09 '16 at 12:23
  • `It displays the toast "Upload Successful"` You better read from that input stream to see what the script echos back. Now you are neglecting the echo. – greenapps Sep 09 '16 at 12:43
  • You can not send binary data using plain SQL. This probably solves a similar problem: http://stackoverflow.com/questions/10729824/how-to-insert-blob-and-clob-files-in-mysql – Boris Schegolev Sep 09 '16 at 13:19
  • I've changed the code to now and show the inputstream from the script - it display this error message: `Parse error: syntax error unexpected $sql_query (T_VARIABLE) on line 8` – Jason Fung Sep 09 '16 at 14:04

0 Answers0