如何使用Java将长Blob(图像)上传到mysql数据库并在php中检索?

如何使用Java将长Blob(图像)上传到mysql数据库并在php中检索?

问题描述:

我正在一个项目中,需要将图像上传到数据库并从数据库中检索图像.

I am working on a project where I need to upload images to the database and retrieve the same from database.

我需要使用Java从Android设备上传图片. 这是我已经实现的

I need the image to be uploaded from an android device using java. Here is what i have implemented

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte[] byte_arr = stream.toByteArray();
            image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

我正在将字节数组插入数据库. 这是我的php代码来检索相同的内容:

I am inserting the byte array to the database. And here is my php code to retrieve the same :

 /**
*Get profile_pic*/
public function callmethod($userId){
    $stmt = $this->conn->prepare("SELECT profile_pic FROM users WHERE unique_id=?");
    $stmt->bind_param('s',$userId); 
    //$result = mysql_query($query) or die(mysql_error()); 
    //$photo = mysql_fetch_array($result); 
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($profile_pic);
    $stmt->fetch();
     //$obj->picture = base64_encode($profile_pic);
    //echo $obj;

    header("Content-Type: image/jpeg");
echo '<img src="data:image/jpeg;base64,<?php echo base64_encode($profile_pic); ?>" />';
}

我在这里面临的问题是文件正在上传到数据库,但是当我从数据库中检索图像时,变量$profile_pic为空,因此图像未显示.

The problem that I am facing here is that the files are getting uploaded to the database but when I am retrieving the image from the database the variable $profile_pic is empty, hence the image is not being displayed.

我需要它能够使用android中的java检索图像.可以仅通过使用json格式对取值进行编码来做到这一点?

I need it to be able to retrieve the image using java in android. can I do that by just encoding the value retrieve with json format?

请让我知道我做错了什么. TIA

Pleas let me know what I am doing wrong. TIA

 /**
*Get profile_pic*/
public function callmethod($userId){
$stmt = $this->conn->prepare("SELECT profile_pic FROM users WHERE unique_id=?");
$stmt->bind_param('s',$userId); 
//$result = mysql_query($query) or die(mysql_error()); 
//$photo = mysql_fetch_array($result); 
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($profile_pic);
while ($stmt->fetch()) {
    echo "<img src='data:image/jpeg;base64,".$profile_pic."' />";
}
 //$obj->picture = base64_encode($profile_pic);
//echo $obj;


}

确定,请尝试此代码.您不需要 header("Content-Type: image/jpeg"); 功能.这是您的php代码中的错误.此代码将使用basc64创建img标签.

ok try this code this.you don't need header("Content-Type: image/jpeg"); function. this are error in your php code. this code will create img tag with basc64.

现在是Android部分.

now for android part.

在php中更改它.

while ($stmt->fetch()) {
    echo "<img src='data:image/jpeg;base64,".$profile_pic."' />";
}

while ($stmt->fetch()) {
    echo $profile_pic;
}

这将是您的android部分.

and this would be your android part.

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
image.setImageBitmap(decodedByte);