Search code examples
androidandroid-cardviewandroid-bitmapmalformedurlexception

Setting an image in CardView using URL


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?


Solution

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

    Or use Intent view with Gallary example