0

I'm trying to load an image from URL into Bitmap object, but I always end up with NetworkInUIThread kind of exception. Can anyone provide me a library for this?

Note, that I'm not trying to put the image into ImageView, I want to put it into Bitmap object first, as it will be used more times later in my app.

I found this code in some other StackOverflow thread, but it doesn't work.

public static Bitmap getBitmapFromURL(String src){
    try{
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    }catch(IOException e){
        return null;
    }
}
Matej
  • 177
  • 2
  • 16
  • 1
    Possible duplicate of [Android : Loading an image from the Web with Asynctask](https://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask) – nhoxbypass Nov 03 '17 at 14:16
  • Possible duplicate of [How do I fix android.os.NetworkOnMainThreadException?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) – Nabin Bhandari Nov 03 '17 at 14:19
  • At first try to read about `NetworkOnMainThreadException` this will facilitate development – Stanislav Bondar Nov 03 '17 at 14:21

4 Answers4

2

Put this code inside the doInBackground method of an AsyncTask

e.g.

private class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
    protected Bitmap doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch(IOException e) {
            return null;
        }
    }

    protected void onPostExecute(Bitmap result) {
         //do what you want with your bitmap result on the UI thread
    }

}

and call it in your activity by:

new MyAsyncTask().execute(src);
pleft
  • 7,567
  • 2
  • 21
  • 45
  • This would work, but I need to get the Bitmap result object in my activity. – Matej Nov 03 '17 at 17:23
  • That’s what you have to do in the onPostExecute... create the MyAsyncTask as inner class inside your activity. read a AsyncTask tutorial for more – pleft Nov 03 '17 at 17:50
1
    try {
        URL url = new URL("http://....");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch(IOException e) {
        System.out.println(e);
    }
1

try

        Bitmap bitmap = null;
        InputStream iStream = null;
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(5000 /* milliseconds */);
        conn.setConnectTimeout(7000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
        iStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(iStream);
JBR
  • 136
  • 9
1

You can use Glide To download image from server and store it in Bitmap Object.

Bitmap theBitmap = Glide.
    with(this).
    load("http://....").
    asBitmap().
    into(100, 100). // Width and height
    get();

Please Refer this to add glide dependency in your app -> https://github.com/bumptech/glide

Arbaz Ali Rizvi
  • 253
  • 1
  • 7