BitmapFactory.decodeStream不起作用

时间:2014-04-16 00:36:42

标签: android bitmapfactory

我在decodeStream返回null时遇到了一些问题。这似乎是一个相当普遍的问题,但它通常被归结为两个问题之一:

  • 尝试在其中完整加载大位图时抛出OutOfMemory异常。
  • 尝试两次使用相同的输入流。

但是,我也没做过。运行它的代码只是

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

将网址设置为here。此代码完成后,image为null。这个问题让我完全疯了 - 它在PNG上工作得很好但似乎在我能给它的每一个BMP下分崩离析,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

最终,使用BufferedHTTPEntity返回的InputStream找到了答案here。虽然它似乎不必要地复杂,但我只能假设只是直接从URL对象获取流不会返回适当类型的流,因此它不能正确读出所有数据。

在问题被删除的情况下交叉发布代码:

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;
}
相关问题