简体   繁体   中英

Java - Another “BitmapFactory.decodeByteArray always null”

I have an image stored in the database in this form

"/9j/4AAQSkZJRgABAQAAAQABAAD/2w <...> igD//2Q=="

so definitely base64 encoded.

I get it via a php function:

...
$query = $db->prepare("SELECT `Data` FROM `UserPicture` WHERE `UserId` = ?");

$query->bind_param('i', $id);
$query->execute();
$query->bind_result($data);
$query->fetch();

echo base64_encode($data);

Android gets it via a call:

StringBuffer result = m_dbi.getResult(Constants.SERVER_HOST_PHP + "/get_user_picture.php", params, "GET");

Debugging I've seen that result is correct.

I pass result.toString() to a writeImageToFile(String pic) that does:

...
byte[] decodedByte = Base64.decode(pic, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
...

pic is correct as well as decodedByte.length but bm is always null .

I've tried to change Base64.DEFAULT to NO_PADDING or NO_WRAP but always null.

What could be the reason ?

The problem was caused because you saved the images in a BLOB using mysqli 's real_escape_string() . This method, like any string-escaping method, is intended for text, and changes content it perceives as invalid characters. Applying this to an image rather than a string corrupted the image.

A BLOB should be stored using the procedure described in this Oracle blog entry , using send_long_data() and no escaping.

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