mysql longblob java,如何使用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.

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

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

}

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.

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?

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

}

//$obj->picture = base64_encode($profile_pic);

//echo $obj;

}

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.

now for android part.

change this in php.

while ($stmt->fetch()) {

echo "";

}

to

while ($stmt->fetch()) {

echo $profile_pic;

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值