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();
This will work,save it as byte[]
array not as Array.toString()
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", b);
I will try to answer your php side. Here I assume, $_POST['image']
as the file input in base64_encode
encoded format. So we will insert it as base64 encoded format to database as blob. Also will use
retrieve the data and decode the image create from string
if (isset($_POST['image'])) { $request5 = "INSERT INTO image_personne (id_personne, isDeleted, image,sModere)
VALUES (?, ?, ?,?)"; //statement for db update
$image = $_POST['image']; //assign image the received data
$id = $_POST['id']; //assign values to variables
$isdel = '0';
$ismod = 0;
$stmt = $connexion->prepare($request5); //prepare the statements
$stmt->bind_param("isbi", $id, $isdel, $null, $ismod ); //bind parameter
$stmt->send_long_data(0, $image); //add blob data to statement
$stmt->execute(); //execute the statement
}
Update:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
You can try this method for image file to string conversion.