Android位图解码问题
我在使用 XML 从服务器检索文件时遇到问题。
位图(GIF)经过 Base64 编码,然后作为字符串放置在 XML 中。 应用程序获取此字符串,将其解码为位图,然后显示它。
大多数情况下它工作正常,但有时 BitmapFactory.decodeByteArray 返回 null,没有任何问题迹象。
我已经通过 XML 复制了一个失败的文件(它是可重复的!)作为资源,并且它工作得很好。
bMapArray = Base64Coder.decode(cd.image); // Decode the encoded string
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.fail);
Log.e("config", bMap.getConfig().name());
// shows RGB_565, decodes OK and will display
bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
Log.e("config", bMap.getConfig().name());
// Null config and displays OK for some files,
// Fails decodeByteArray for the file matching the one used as a resource and some others
我花了几天时间试图解决这个问题,但没有成功。有人有好主意吗? 我很高兴接受这是 64 位编码/解码过程的问题,但由于解码字节数组没有错误,我不确定是什么。
干杯,
马丁。
I have a problem with a file retrieved from a server using XML.
The bitmap (a GIF) is base64 encoded then placed inside the XML as a string.
The app gets this string, decodes it to a bitmap, then displays it.
It works fine most of the time, but sometimes the BitmapFactory.decodeByteArray returns null, with no indication of the problem.
I've copied one of the files that fails via XML (it's repeatable!) as a resource, and it works just fine.
bMapArray = Base64Coder.decode(cd.image); // Decode the encoded string
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.fail);
Log.e("config", bMap.getConfig().name());
// shows RGB_565, decodes OK and will display
bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
Log.e("config", bMap.getConfig().name());
// Null config and displays OK for some files,
// Fails decodeByteArray for the file matching the one used as a resource and some others
I've spent days trying to fathom out the problem with no success. Does anyone have a bright idea?
I'm happy to accept it's a problem with the 64 bit enc/decode process, but with no errors from decodeByteArray I'm not sure what.
Cheers,
Martin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个应用程序可以从各种站点下载位图 - 这些站点都不是 base64 编码的。大多数时候,位图通过decodeByteArray 就很好,但时不时地(例如默认的Facebook 个人资料图标)我会返回null。
我花了很长时间试图弄清楚发生了什么,最后得出结论(正如你也怀疑的那样)这是decodeByteArray 中 GIF 处理的一个错误。
如果您在这里(和其他地方)搜索“android bitmap gif Decodebytearray”,您可以找到很多其他人也遇到同样问题的提示 - 但我还没有找到权威的错误报告。
I have an app that downloads bitmaps from a variety of sites - none of which are base64 encoded. Most of the time the bitmaps go through decodeByteArray just fine, but every now and again (eg the default Facebook profile icon) I get back null.
I spent ages trying to figure out what was going on, and in the end concluded (as you also suspect) that it was a bug in GIF handling in decodeByteArray.
If you search here (and elsewhere) for "android bitmap gif decodebytearray" you can find lots of hints that other people are having the same issue - but I haven't found a authorative bug report.
这里有三个非常明显的地方可能会出错。
1) 发送前将图像添加到 XML 的方式 - 检查是否正确编码
2) 传输过程 - 确保传输没有被截断,特别是对于缓冲输入流
3) 解析来自收到的 XML 的数据 - 再次检查字符串是否已正确解析。
最有可能的候选者是传播。图像的大小会影响您看到的结果吗?如果它只接收部分数据,它将失败。
There are three very obvious things that can go wrong here.
1) The way the image is added to the XML before sending - check to see if it's being encoded properly
2) The transmission process - make sure that the transmission isn't being truncated, especially with buffered input streams
3) The parsing of the data from the XML recieved - again check that the string is parsed back out properly.
The most likely candidate is transmission. Does the size of image affect the results you are seeing? If it is only recieving part of the data it will fail.