2013年7月25日 星期四

What if BitmapFactory.decodeStream() returns null

If you find an exception error like:

BitmapFactory.decodeStream return null

It could be caused by the following issues:
1)  The image is null, or not exist
2) Surf through the internet and find some people mentioning using BufferedHttpEntity() before calling getContent() is a better way to resolve "sometimes null return" issue

The following shows the code difference between adding BufferedHttpEntity or not:
             InputStream result = null;

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            result = httpEntity.getContent();


------------------------------------------------------------------------
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(httpEntity);
            result = bufHttpEntity.getContent();


Reference links
http://stackoverflow.com/questions/5832229/problem-loading-some-dynamic-png-image-bitmapfactory-decodestream-retuns-null
https://groups.google.com/forum/#!topic/android-developers/EKOCEVjxe-E
https://groups.google.com/forum/#!topic/android-developers/FxuL81277ZY

沒有留言:

張貼留言