dongluanban3536 2015-01-16 13:28
浏览 190
已采纳

Java - 另一个“BitmapFactory.decodeByteArray始终为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 ?

  • 写回答

1条回答 默认 最新

  • dongzhuang6177 2015-01-16 22:51
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题