本文介绍了BitmapFactory.de codeStream不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点与去codeStream返回null的问题。这似乎是围绕一个相当普遍的问题,但它通常牵制到两个问题之一:

I'm having a bit of a problem with decodeStream returning null. It seems to be a fairly common problem around, but it's usually pinned down to one of two problems:


  • 一个OutOfMemory例外通过尝试在它全部加载一个大的位图异常。

  • 试图使用相同的输入流的两倍。

不过,我没有做任何。在code运行它仅仅是

However, I'm not doing either. The code to run it is simply

stream = new java.net.URL(url).openStream();
Bitmap image = BitmapFactory.decodeStream(stream);
stream.close();

设置为。图像为null在此之后code是完整的。这个问题一直让我完全疯了 - 它工作正常的PNG图片,但似乎分崩离析每BMP下我可以给它,所以任何帮助将是AP preciated

with the URL set to here. image is null after this code is complete. This issue's been driving me completely insane - it works fine on PNGs but seems to fall apart under every BMP I can give it, so any help would be appreciated.

推荐答案

最后,得到的答案被发现<一个href=\"http://stackoverflow.com/questions/4414839/bitmapfactory-de$c$cstream-returns-null-without-exception?rq=1\">here,使用由BufferedHTTPEntity返回一个InputStream。虽然看起来不必要的复杂,我只能假设,仅仅让从URL对象的流直接不返回适当类型的流,所以它是不正确读出所有的数据。

Ultimately, the answer was found here, using an InputStream returned by a BufferedHTTPEntity. While it seems needlessly complex, I can only assume that simply getting a stream from the URL object directly doesn't return a stream of the appropriate type, and so it wasn't reading out all the data properly.

跨张贴code的情况下,这个问题被删除:

Cross-posting the code in case the question is erased:

private static InputStream fetch(String address) throws MalformedURLException,IOException {
    HttpGet httpRequest = new HttpGet(URI.create(address) );
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    InputStream instream = bufHttpEntity.getContent();
    return instream;
}

这篇关于BitmapFactory.de codeStream不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:31