使用 URL 在 CardView 中设置图像

Setting an image in CardView using URL

我正在创建一个 CardView。我已经创建了 Adapter 并且它运行良好。当我在 CardView 中为我的 ImageView 初始化 Bitmap 时,我收到一条红线,并在该行下显示一条错误消息:

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

我收到的消息是这样的:

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

我搜索了其他有同样错误的帖子,但他们的解决方案并没有解决我的问题。我试过将该行放在 trycatch 子句中,如下所示:

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

我得到了同样的错误。语句 list.get(position).thumbnail 的类型为 String。为什么会出现此错误?我该如何解决这个问题?

这是 URI,不是 URL。如果你想加载它,请阅读答案 link.

或者将 Intent 视图与图库一起使用 example