【问题标题】:Bitmap decode stream from URL return nulled (SkImageDecoder::Factory returned null)来自 URL 的位图解码流返回 null(SkImageDecoder::Factory 返回 null)
【发布时间】:2014-02-20 15:17:46
【问题描述】:

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

来自本准则

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);

它工作正常。

【问题讨论】:

    标签: android bitmap inputstream


    【解决方案1】:

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

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

    【讨论】:

    • 你能显示代码吗,如何重新打开新的流?谢谢
    • @ipOP :在使用之前,您需要执行两次请求或将图像保存到缓存(在内存或磁盘上)。
    • BufferedHttpEntities 已被弃用,现在怎么办?
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多