开发者

BitmapFactory.decodeStream(InputStream is) returns null for non null InputStream on Android

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 NO开发者_JS百科T 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!


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


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜