Android Bitmap.class中NativeDecodeByteArray的问题

Sla*_*ast 5 java base64 android

我在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;
Run Code Online (Sandbox Code Playgroud)

对于失败的图像,它会通过

也许我们得到了幸运的检查,并返回decoder.output并直接跳转到返回temp,其中inturn返回null

.但是对于有效的图像,如果并且返回非空的临时变量,则不会输入该图像.这是否存在已知问题?

UPDATE

调用代码

 //This decodedString is null
    byte[] decodedString = Base64.decode(
                        data, Base64.DEFAULT);
                Bitmap setBMPPath = BitmapFactory.decodeByteArray(decodedString, 0,
                        decodedString.length);

                qImage.setImageBitmap(setBMPPath); 
Run Code Online (Sandbox Code Playgroud)

编辑

原来该方法返回一个值.谢谢@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;
    }
Run Code Online (Sandbox Code Playgroud)

这里bm返回null!基本上这个电话

Bitmap bm = nativeDecodeByteArray(data, offset, length, opts);
Run Code Online (Sandbox Code Playgroud)

是null但是因为opts != null && opts.inBitmap != null不是真的它不抛出IllegalArgumentException并且只返回bm为null.有什么想法吗?

UPDATE

我在日志中看到一个错误Skia --decoder decode returns false.我已经尝试了所有关于SO的问题的答案,但它仍然没有用.

我的Base64代码:http://pastebin.com/pnbqqg97

如果它发布在一个在线解码的网站上,它会喷出正确的图像,但位图解码器会失败.

更新 - 解决我的问题

事实证明,二进制编码是在PNG文件上完成的,然后被重新转换回bmp文件,bmp到bmp似乎在所有情况下都能正常工作.但我很惊讶一些png转换也工作!因为尺寸问题,让我把原始图像作为bmp是个问题,有没有办法可以在不使用BitMap工厂的情况下可靠地解码Android中的PNG base 64?

回答

原来图像在一个字符点实际上是无效的:O但是由于某种原因它没有被在线解码器拾取但是在上面显示的代码中.确保检查代码以确定您没有替换任何图像base64字符.正如我发现的那样,即使是一次性也可以让它变得混乱

Dav*_*ann 0

看看为什么 BitmapFactory.decodeByteArray 返回 null?。有点奇怪的是,您粘贴的数据看起来像一个有效的 PNG。