【问题标题】:BitmapFactory.decodeStream(InputStream is) returns null for non null InputStream on AndroidBitmapFactory.decodeStream(InputStream is) 在 Android 上为非 null InputStream 返回 null
【发布时间】:2011-08-21 21:08:12
【问题描述】:

我正在开发一个 Android 应用程序,它的视图包含多个图库。画廊(位图)的内容是来自互联网的红色。

对于第一个画廊,一切正常,但是当尝试下载第二个画廊的第一张图片时,BitmapFactory.decodeStream(InputStream) 返回 null,而流不为 null。

public void loadBitmap() throws IOException {

        for (int i = 0; i < images.size(); ++i) {
            URL ulrn = new URL(images.get(i).getThumbUrl());
            HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
            InputStream is = con.getInputStream();
            images.get(i).setImage(BitmapFactory.decodeStream(is));
            Log.i("MY_TAG", "Height: " + images.get(i).getImage().getHeight());
        }
}

getThumbUrl() 返回图片的 URL(例如http://mydomain.com/image.jpg) 它在Log.i("MY_TAG", "Height: ... ) 行抛出一个NullPointerExceptionimages 是一个 ArrayList 包含我的类的对象,它也包含 URL 和位图)。

感谢您的建议!

【问题讨论】:

  • 不太大,我会说大约 100x50。但如果这是问题所在,我会得到一些 OutOfMemoryException,不是吗? (我将大约 30-35 张图片加载到画廊,一次大约 10 张大一点的图片)

标签: android gallery bitmapfactory


【解决方案1】:

我遇到过这个。尝试将 BufferedHttpEntity 与您的输入流一起使用。我发现这避免了 99.9% 的从 decodeStream 获取静默空值的问题。

也许并不重要,但我可靠地使用 org.apache.http.client.HttpClient 而不是 HttpURLConnection,如下所示:

public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)
{
    HttpResponse response=null;
    Bitmap b=null;
    InputStream instream=null;

    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    decodeOptions.inPreferredConfig = bitmapCOnfig;
    try
    {
    HttpGet request = new HttpGet(url.toURI());
        response = client.execute(request);
        if (response.getStatusLine().getStatusCode() != 200)
        {
            MyLogger.w("Bad response on " + url.toString());
            MyLogger.w ("http response: " + response.getStatusLine().toString());
            return null;
        }
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
        instream = bufHttpEntity.getContent();

        return BitmapFactory.decodeStream(instream, null, decodeOptions);
    }
    catch (Exception ex)
    {
        MyLogger.e("error decoding bitmap from:" + url, ex);
        if (response != null)
        {
            MyLogger.e("http status: " + response.getStatusLine().getStatusCode());
        }
        return null;
    }
    finally
    {
        if (instream != null)
        {
            try {
                instream.close();
            } catch (IOException e) {
                MyLogger.e("error closing stream", e);
            }
        }
    }
}

【讨论】:

  • 是的,它解决了,非常感谢,也感谢你的代码!
  • 嗯...我在一个视图中成功使用了上面的 2 个画廊。你遇到了什么错误?
  • 我没有收到任何错误,返回的位图只是空的。这很奇怪,因为我在代码中包含了错误消息。
  • 现在应用程序加载了所有图像,但我收到以下错误:'05-10 10:31:19.553: ERROR/dalvikvm-heap(26407): 582400-byte external allocation too large for这个流程。'我认为它与内存问题有关,但为什么我没有得到异常?
  • 奇怪.. 'return BitmapFactory.decodeStream(instream, null, decodeOptions);'这一行现在抛出 OutOfMemoryException,但它未处理(应用程序崩溃),尽管有 catch 块
【解决方案2】:

Google 把我带到了这里。对于每个有同样问题的人:

问题: http://code.google.com/p/android/issues/detail?id=6066

解决方案(“FlushedInputStream”): http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

【讨论】:

  • FlushedInputStream 对我不起作用。 @mmeyer 解决方案确实做到了。
【解决方案3】:
public static Bitmap decodeStream (InputStream is)

返回

解码后的位图,如果图像数据无法解码,则为null。

您是否检查过您没有收到一些 404 错误或类似错误,而不是图像?

【讨论】:

  • 嗨!是的,我注销了 url,复制到浏览器,浏览器打开了图像。
  • 检查con.getResponseCode();的值为200(HTTP_OK)
  • 是的,它是 200,所以流似乎没问题。
猜你喜欢
  • 2017-03-10
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多