0

I am trying to download images from server to be used as map markers icon in my android app.

This is how I am trying to download the images:

  public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

And this is how I am calling this method:

 String urlbitmap = "https://faro.red/buju/administrar/application/admin/usuarios/" + foto;
            Log.d("RES SUVJETC","numProfesionales urlbitmap:"+urlbitmap);
            Bitmap postbitmap = getBitmapFromURL(urlbitmap);

For the variable urlbitmap, this is the log output:

https://faro.red/buju/administrar/application/admin/usuarios/blue-user-icon.png

The image is at this URL.

I am getting an exception at line:

InputStream input = connection.getInputStream();

The exception output is this:

19-07-26 14:25:41.984 20497-20497/com.mpidesarrollo.buju E/AsyncHttpRH: User-space exception detected!
    android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
        at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:300)
        at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:839)
        at com.android.okhttp.okio.Okio$1.write(Okio.java:81)
        at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
        at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
        at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
        at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:60)
        at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:1154)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:976)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:509)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:247)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
        at com.mpidesarrollo.buju.HomeFragment.getBitmapFromURL(HomeFragment.java:355)
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • You are performing your operation in main thread that's why you are getting error. To overcome you should use _AsyncTask_ and call _getBitmapFromURL()_ in `doInBackground()` method. – Piyush Jul 26 '19 at 12:45
  • @Piyush, would you mind giving me an example on how to use AsyncTask in this case? – mvasco Jul 26 '19 at 12:50
  • The best way to use glide or picasso library for async https://square.github.io/picasso/ https://github.com/bumptech/glide – Anas Mehar Jul 26 '19 at 12:52
  • Check this [1](https://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask). And You can use Glide which have a callback which will give you bitmap in return. – Piyush Jul 26 '19 at 12:52

1 Answers1

-1

I have the same problem. It solved by changing the url protocol from https to http