2015-09-25 94 views
0

大家好!正如標題所說,我嘗試將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(); 
+0

現在,因爲我更正了一個部分(請參閱其他答案),它看起來像是在php中。我不知道如何在blob中轉換我的byte [] – dam

回答

0

這將工作,它不保存爲byte[]陣列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); 
+0

謝謝你的回答。我試過了,它在php文件中返回一個錯誤:Warning:imagecreatefromstring():空字符串或無效圖像。所以我想我必須刪除imagecreatefromstring並只寫:$ image = base64_decode($ _ POST ['image']);但它返回另一個錯誤:您的SQL語法中有錯誤;在第1行檢查對應於你的MySQL服務器版本的手冊,以找到在'?w?5,0'附近使用的正確語法。'我猜圖像格式不好。你知道我必須做什麼嗎? – dam

0

我會盡量回答你的php方面。這裏我假設,$_POST['image']作爲base64_encode編碼格式的文件輸入。所以我們會以blob的形式將它作爲base64編碼格式插入到數據庫中。還將利用

  1. 我們收到的數據base64_encod格式
  2. 將它保存在數據庫中的BLOB
  3. 檢索數據和圖像解碼來自串

    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 
        } 
    

更新創建:

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); 

您可以嘗試使用此方法對圖像文件進行字符串轉換。

+0

好吧,我嘗試了,但現在當我插入在MySQL中的圖像爲空...你沒有做這樣的錯誤:「bind_param(」isbi「,$ id,$ isdel,$ null,$ ismod);」 ?是不是$ image而不是$ null? Thx – dam

+0

$ stmt-> send_long_data(0,$ image);這會在執行過程中將數據添加到數據庫。順便可以顯示數據發送代碼/發送數據類型。 @dam –

+0

但是,php如何知道在send_long_data中發送$ image的位置?事實上,它可能是第一個字段,第二個字段,第三個字段......或者它是否檢測到您在bind_param中放置了「$ null」的地方?在MySQL中:id_personne是INT(11),isDeleted是TINYINT(1),圖像是BLOB,isModere是TINYINT(1) – dam