我正在Android(Java)上解码64幅编码的图像。它适用于大多数图像,但对于某些图像二进制文件,它返回null。如果我使用一个在线解码器,所讨论的二进制文件可以正常工作,这表明格式是正确的。
Base64.class文件中出错的代码是
if (!decoder.process(input, offset, len, true)) {
throw new IllegalArgumentException("bad base-64");
}
// Maybe we got lucky and allocated exactly enough output space.
if (decoder.op == decoder.output.length) {
return decoder.output;
}
// Need to shorten the array, so allocate a new one of the
// right size and copy.
byte[] temp = new byte[decoder.op];
System.arraycopy(decoder.output, 0, temp, 0, decoder.op);
return temp;对于失败的图像,它将通过
也许我们得到了幸运的检查,并返回decoder.output并直接跳转返回temp,后者返回null。
。但是对于正常工作的图像,它不会输入这个if并返回一个非空的临时变量。这有什么已知的问题吗?
更新
调用代码
//This decodedString is null
byte[] decodedString = Base64.decode(
data, Base64.DEFAULT);
Bitmap setBMPPath = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
qImage.setImageBitmap(setBMPPath); 编辑
结果是该方法正在返回一个值。谢谢@DavidEhrmann的帮助。这个错误实际上在下一个步骤中,我将解码字符串转换为位图。
public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) {
if ((offset | length) < 0 || data.length < offset + length) {
throw new ArrayIndexOutOfBoundsException();
}
Bitmap bm = nativeDecodeByteArray(data, offset, length, opts);
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
return bm;
}这里bm返回空!基本上这个电话
Bitmap bm = nativeDecodeByteArray(data, offset, length, opts);是空的,但是由于opts != null && opts.inBitmap != null不是真,所以它不会抛出IllegalArgumentException,只会将bm作为null返回。知道为什么吗?
更新
我在日志中看到一个错误,上面写着Skia --decoder decode returns false。我已经试过每一个这样的问题的每一个答案,但它仍然不起作用。
我的Base64代码:http://pastebin.com/pnbqqg97
如果它发布在一个在线解码的网站上,它就会发出正确的图像,但是位图解码器失败了。
更新-解决我的问题
事实证明,二进制编码是在PNG文件上执行的,然后被重新转换回bmp文件,bmp到bmp似乎在所有情况下都有效。但我是惊奇的一些巴布亚新几内亚的转换也工作!对于我来说,BUt作为bmp的原始图像是一个问题,因为它的大小,有没有一种方法,我可以解码一个PNG基地64在安卓可靠而不用BitMap factory>?
答案
实际上,图像在一个字符点上是无效的:O,但由于某种原因,它没有被在线解码器接收,而是出现在上面所示的代码中。请确保检查代码,以确保没有替换任何图像base64字符。即使是一个关也会让它像我发现的那样发昏。
发布于 2014-03-24 16:45:09
看看Why does BitmapFactory.decodeByteArray return null?。奇怪的是,您所存储的数据看起来像一个有效的PNG。
https://stackoverflow.com/questions/22604116
复制相似问题