来自 URL 的位图解码流返回空 (SkImageDecoder::Factory 返回空)



我们尝试将图像流解码为位图,但它返回为空。

从本代码

URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
options.inSampleSize = calculateInSampleSize(options, 768, 1280);
options.inJustDecodeBounds = false;
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis, options);
bis.close();
is.close();

我们得到日志猫

SkImageDecoder::Factory returned null

但是当我们只使用时

InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);

它工作正常。

我最近遇到了类似的问题。 分两次解码InputStream时出现问题(首先是图像边界,然后是实际解码),第一次传递后InputStream没有重置 - 这在我的情况下导致了错误。 为了解决这个问题,我只是在第一次传递后重置InputStream,方法是关闭用于获取图像边界的原始流,然后在执行实际Bitmap解码之前重新打开新流。

这解决了我的情况中的问题,但这是一个相当普遍的问题。 如果执行上述操作不起作用 - 可能值得研究一下这个谷歌代码问题,或者这篇关于使用BufferedHttpEntities的SO帖子。

我尝试在将inJustDecodeBounds设置为FALSE之前添加以下代码

//... calculateInSampleSize
is.close();
conn = aURL.openConnection();
conn.connect();
is = conn.getInputStream();
options.inJustDecodeBounds = false;

它工作正常,但我不确定这是解决我问题的最佳方法

最新更新