我正在开发一个项目,我需要将图像上传到数据库并从数据库中检索相同的项目。如何使用java将长blob(图像)上传到mysql数据库并在php中检索?
我需要使用java从android设备上传图像。 以下是我已经实现
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代码检索相同:
/**
*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
是空的,因此图像没有被显示。
我需要它能够在android中使用java检索图像。我可以通过编码json
格式的值检索吗?
请让我知道我做错了什么。 TIA
答案是你通过将文件存储在数据库中使你的生活不必要地复杂化。它更简单,更高效(从编码和存储/检索的角度来看)将文件存储在文件系统上。 – e4c5
请让我想想做什么 – silverFoxA