Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is BitmapFactory.decodeStream returning null?

Basically I am parsing some JSON that has an image with it and trying to load it into a ImageView. However mBitmap is returning null. I have no idea why and further research has not helped..

Here is an example url I am working with:

http://b.thumbs.redditmedia.com/ivBAJzLMJEkEy9jgTy3z4n-mO7gIGt5mQFU1Al5kJ-I.jpg

Here is all relevant code:

 public static Bitmap LoadImageFromUrl(String url){
   try {
      mBitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
       return mBitmap;
    }catch (Exception e){
       Log.d(TAG,"Error getting image");
       return null;
   }
}

Here is where the method is called:

mListingModel.setmImageView(LoadImageFromUrl(data.getString(JSON_THUMBNAIL)));

Here is where I set the ImageView:

    if(mItem.getmImageView() != null) {
        holder.imageView.setImageBitmap(mItem.getmImageView());
    }

Note: I am calling the method in an AsyncTask so that is not the problem.

like image 890
MrTimotheos Avatar asked Jun 19 '26 09:06

MrTimotheos


1 Answers

The javadoc for Bitmap.decodeStream() states that:

If the input stream is null, or cannot be used to decode a bitmap, the function returns null

You're passing it the results of URL.getContent(), which states in its javadoc:

By default this returns an InputStream, or null if the content type of the response is unknown.

So maybe you should check to see if getContent() returns null before passing it on to the decoder.

like image 91
Doug Stevenson Avatar answered Jun 21 '26 23:06

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!