Good day everybody! As the title say, I try to load an Image from an ImageView to MySQL. I want to save it like a Blob in MySQL.
I have seen several questions about this but no answers in my case: I still remain stuck!
Following is my XML ImageView:
<ImageView
android:id="@+id/das_photoib"
android:layout_width="150dp"
android:layout_height="150dp"
android:maxWidth = "150dp"
android:maxHeight="150dp"
android:layout_gravity="center_horizontal"/>
Following is my JAVA activity
rootView = inflater.inflate(R.layout.devenirassistant, container, false);
photoprofil = (ImageView) rootView.findViewById(R.id.das_photoib);
In the same activity, when I want to upload the picture, I put it in a ContentValues:
ContentValues cv = new ContentValues();
Bitmap bitmap = ((BitmapDrawable)photoprofil.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
cv.put("image", Arrays.toString(b));
Then I call an AsyncTask which call a php file itself with the contentValues as parameter. In the php file I get the "image" data and I try to load it in the MySQL "image" row of a table "image_personne", which has been define as "BLOB". I do it like this:
if (isset($_POST['image'])){
$image = imagecreatefromstring(base64_decode($_POST['image']));
$request5 = "INSERT INTO image_personne (id_personne, isDeleted, image, isModere) VALUES ({$_POST['id']}, '0', $image, 0)";
$result5 = mysqli_query($connexion,$request5) or die(mysqli_error($connexion));
}
There are no errors out, but that doesn't load! Could you help me to debug? Thank you!
This, is what is done now:
Android side:
ContentValues cv = new ContentValues();
Bitmap bitmap = ((BitmapDrawable)photoprofil.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
cv.put("image", Base64.encode(b,Base64.DEFAULT));
Php side:
$request5 = "INSERT INTO image_personne (id_personne, isDeleted, image, isModere) VALUES (?,?,?,?)";
$image = $_POST['image'];
$id = $_POST['id'];
$del = '0';
$mod = '0';
$stmt = $connexion->prepare($request5); //prepare the statements
$stmt->bind_param("isbi", $id, $del, $null, $mod ); //bind parameter
$stmt->send_long_data(0, $image); //add blob data to statement
$stmt->execute();