大家好!正如標題所說,我嘗試將ImageView從ImageView加載到MySQL。我想在MySQL中將它像Blob一樣保存。從Android到MySQL的圖像Blob
我已經看到了幾個關於這個問題,但在我的情況沒有答案:我仍然堅持!
以下是我的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"/>
以下是我的JAVA活動
rootView = inflater.inflate(R.layout.devenirassistant, container, false);
photoprofil = (ImageView) rootView.findViewById(R.id.das_photoib);
在同一個活動,當我要上傳的圖片,我把它放在一個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));
然後我調用一個AsyncTask,它調用一個php文件本身,contentValues作爲參數呃。 在php文件中,我得到「圖像」數據,並嘗試將它加載到表「image_personne」的MySQL「image」行中,該表已被定義爲「BLOB」。我這樣做:
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));
}
有沒有錯誤,但沒有加載!你能幫我調試嗎?謝謝!
這是什麼現在做:
Android的一面:
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端:
$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();
現在,因爲我更正了一個部分(請參閱其他答案),它看起來像是在php中。我不知道如何在blob中轉換我的byte [] – dam