从Android到MySQL的图像Blob

问题描述:

大家好!正如标题所说,我尝试将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

这将工作,它不保存为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

我会尽量回答你的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