-5

the image did not appear at all , there is an exception is always caught by try catch and the image has never been appeared

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();
}
Jens
  • 20,533
  • 11
  • 60
  • 86
Osama Aldawoody
  • 11
  • 1
  • 4
  • 9

1 Answers1

0

I used this line of code in mainifest file

android:usesCleartextTraffic="true"

and this my code solution using background thread

private class imageAsyncTask extends AsyncTask{ @Override protected Bitmap doInBackground(String... url) { if (url.length < 1 || url[0] == null){ return 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);
    }
} }
Osama Aldawoody
  • 11
  • 1
  • 4
  • 9