0

I am creating a CardView. I have created the Adapter and it is working perfectly. When I initialise the Bitmap for my ImageView within the CardView, I get a red line and an error message under this line:

viewHolder.thumbnail.setImageBitmap(BitmapFactory.decodeStream(new URL(list.get(position).thumbnail).openConnection().getInputStream()));

The message I am receiving is this:

error: unreported exception MalformedURLException; must be caught or declared to be thrown

I have searched through other posts with the same error but their resolutions don't solve my problem. I have tried putting that line within a try and catch clause like so:

try {
   viewHolder.thumbnail.setImageBitmap(BitmapFactory.decodeStream(new URL(list.get(position).thumbnail).openConnection().getInputStream()));
} catch (MalformedURLException e) {
    e.printStackTrace();
}

I get the same error. The statement list.get(position).thumbnail is of type String. Why am I getting this error? How do I solve the issue?

  • 2
    I think you should use third-party for loading image. Ex : Glide, Picasso... – Iris Louis Jun 30 '16 at 01:50
  • Can you show logcat URLs get from list ? – Iris Louis Jun 30 '16 at 01:52
  • The URL's I get are from images from the Gallery. They have funny URLs and looked like this: `/external/images/media/3666`. – Stein Sigmund Jun 30 '16 at 01:59
  • This is URI not URL. If you want load it, plz read anwser [link](http://stackoverflow.com/a/3005936/3832212) – Iris Louis Jun 30 '16 at 02:05
  • I have read it. I'm not getting any errors but the image is still not appearing. Is it because I have no file extension? – Stein Sigmund Jun 30 '16 at 02:11
  • Right, try again with extension. – Iris Louis Jun 30 '16 at 02:13
  • That's the problem. I have an `Intent` in another activity that selects an image from the gallery. The `Uri` that the `Intent` returns doesn't contain a file extension. My `viewHolder` uses that `Uri` to display it in the `ImageView`. Do I need to find the path of that image instead rather that `Uri`? – Stein Sigmund Jun 30 '16 at 02:16
  • Now, you want show image or... ??. If Intent return doesn't contain a file extension, you can handle with String path (concat(.file extension)). Use Intent view with Gallary [example](http://stackoverflow.com/a/5384265/3832212) – Iris Louis Jun 30 '16 at 02:20
  • @Iris Louis Yes! Thank you very much! It works perfectly! – Stein Sigmund Jun 30 '16 at 02:27
  • You're Wellcome @@~. Can you vote answer for me ? :) – Iris Louis Jun 30 '16 at 02:28

1 Answers1

0

This is URI not URL. If you want load it, plz read answer link.

Or use Intent view with Gallary example

Community
  • 1
  • 1
Iris Louis
  • 297
  • 6
  • 19