我正在創建CardView
。我創建了Adapter
,它工作正常。當我初始化Bitmap
我ImageView
的CardView
中,我得到這個項目下紅線和一條錯誤消息:使用URL在CardView中設置圖像
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
我已搜查通過其他職位相同的錯誤,但他們的決議並不能解決我的問題。我試圖把一個try
和catch
條款內的第一行:
try {
viewHolder.thumbnail.setImageBitmap(BitmapFactory.decodeStream(new URL(list.get(position).thumbnail).openConnection().getInputStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
}
我得到同樣的錯誤。聲明list.get(position).thumbnail
的類型爲String
。爲什麼我得到這個錯誤?我如何解決這個問題?
我認爲你應該使用第三方加載圖像。例如:Glide,Picasso ... –
你可以顯示logcat URL從列表中獲取嗎? –
我得到的URL來自圖庫中的圖像。他們有有趣的網址,看起來像這樣:'/ external/images/media/3666'。 –