图像根本没有出现,总是有一个异常被try catch捕获,而图像却从未出现过
URL url = null;
try {
url = new URL(currentBook.getImageLink());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
bookImageView.setImageBitmap(bmp);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
我在维护文件中使用了这一行代码
android:usesCleartextTraffic =“ true”
这是我的使用后台线程的代码解决方案
私有类imageAsyncTask扩展了AsyncTask { @Override 受保护的位图doInBackground(String ... url){ if(url.length <1 || url [0] == null){ 返回null; }
URL realyUrl = null; try { realyUrl = new URL(url[0]); bmp = BitmapFactory.decodeStream(realyUrl.openConnection().getInputStream()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bmp; } @Override protected void onPostExecute(Bitmap bitmap) { if (bitmap != null){ bookImageView.setImageBitmap(bitmap); } } }