BitmapFactory.decodeStream(InputStream is) 对于 Android 上的非 null InputStream 返回 null

发布于 2024-11-06 04:12:23 字数 963 浏览 0 评论 0原文

我正在开发一个 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< /a>) 它会在 Log.i("MY_TAG", "Height: ... ) 行抛出一个 NullPointerExceptionimages 是一个 ArrayList,包含我的类的对象,它也包含 URL 和位图)。

感谢您的任何建议!

I'm developing an Android application, and it's view is containing multiple Gallerys. The content of the Gallerys (the Bitmaps) are red from the Internet.

For the first gallery, everything works fine, but when trying to download the first image of the second Gallery, the BitmapFactory.decodeStream(InputStream) returns null, while the stream is NOT 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());
        }
}

The getThumbUrl() returns the URL of the image (eg. http://mydomain.com/image.jpg)
and it throws a NullPointerException at the line Log.i("MY_TAG", "Height: ... )
(images is an ArrayList containing objects of my class, that holds the URL and the Bitmap too).

Thanks for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星軌x 2024-11-13 04:12:23

我遇到过这个。尝试将 BufferedHttpEntity 与您的输入流一起使用。我发现这可以防止 99.9% 的从解码流获取静默空值的问题。

也许并不重要,但我可靠地使用 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);
            }
        }
    }
}

I've run into this. Try using BufferedHttpEntity with your inputstream. I found this prevented 99.9% of the issues with getting silent nulls from decodeStream.

Maybe not signficant, but I reliably use org.apache.http.client.HttpClient rather than HttpURLConnection as in:

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);
            }
        }
    }
}
等待我真够勒 2024-11-13 04:12:23

谷歌把我带到了这里。对于遇到同样问题的每个人:

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

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

Google brought me here. For everyone having the same problem:

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

Solution ("FlushedInputStream"):
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

嘿嘿嘿 2024-11-13 04:12:23
public static Bitmap decodeStream (InputStream is)

返回

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

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

public static Bitmap decodeStream (InputStream is)

Returns

The decoded bitmap, or null if the image data could not be decoded.

Did you check that you're not getting some 404 error or similar instead of the image?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文