简体   繁体   中英

Android Bitmap to base64 encode

I'm choosing an image, then converting it to base64 encode and sending to my server using Google Volley Request with POST method.

After sending the string to my server I'm checking the image using online image decoder tools, but I can't see the fully image. Only the half of it.

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    return Base64.encodeToString(imageBytes, Base64.DEFAULT);
}

In PHP it looks like this.

$stmt = $mysqli->prepare('INSERT INTO posts (ownerid, image, timestamp) VALUES (?, ?, ?)');
$stmt->bind_param('dsd', $uid, $image, $time);

And here when I try to read the image.

$stmt = $mysqli->prepare("SELECT image FROM posts WHERE id = ?");
$stmt->bind_param('d', $id);
$stmt->execute();
$stmt->bind_result($image);
$stmt->fetch();
$data = base64_decode($image);
$img = imagecreatefromstring($data);
imagejpeg($img);
imagedestroy($img);

PHP Error

Warning: imagecreatefromstring(): gd-png: fatal libpng error: Read Error: truncated data in image.php on line 35
Warning: imagecreatefromstring(): gd-png error: setjmp returns error condition in image.php on line 35
Warning: imagecreatefromstring(): Passed data is not in 'PNG' format in image.php on line 35
Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in image.php on line 35
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in image.php on line 36
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in image.php on line 37

EDIT: And here's my table.

在此处输入图片说明

Blob was too small for my images. I have changed it to Mediumblob and it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM