Error showing image from internet on android [duplicated]

1

I'm trying to show an image from the internet with the following code:

try {
    final ImageView prueba2 = (ImageView) view.findViewById(R.id.pruebaImagen);

    URL url = new URL("http://www.expertoanimal.com/es/images/9/7/5/img_nombres_para_perros_originales_y_bonitos_5579_paso_1_600.jpg");

    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    prueba2.setImageBitmap(bmp);
} 
catch (IOException e) {
    e.printStackTrace();
}

And I get the following error:

  

Process: com.dedicables.videos.dedicable, PID: 24788                                                                                    android.os.NetworkOnMainThreadException                                                                                        at   android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork (StrictMode.java:1145)                                                                                        at java.net.InetAddress.lookupHostByName (InetAddress.java:385)                                                                                        at java.net.InetAddress.getAllByNameImpl (InetAddress.java:236)                                                                                        at java.net.InetAddress.getAllByName (InetAddress.java:214)                                                                                        at com.android.okhttp.internal.Dns $ 1.getAllByName (Dns.java:28)                                                                                        at   com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress (RouteSelector.java:216)                                                                                        at   com.android.okhttp.internal.http.RouteSelector.next (RouteSelector.java:122)                                                                                        at   com.android.okhttp.internal.http.HttpEngine.connect (HttpEngine.java:292)                                                                                        at   com.android.okhttp.internal.http.HttpEngine.sendSocketRequest (HttpEngine.java:255)                                                                                        at   com.android.okhttp.internal.http.HttpEngine.sendRequest (HttpEngine.java:206)                                                                                        at   com.android.okhttp.internal.http.HttpURLConnectionImpl.execute (HttpURLConnectionImpl.java:345)                                                                                        at   com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse (HttpURLConnectionImpl.java:296)                                                                                        at   com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream (HttpURLConnectionImpl.java:179)                                                                                        at   com.dedicables.videos.dedicable.Views.Login.Login.onCreateView (Login.java:92)

Where the error marks me on the following line:

Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

I have tried with several url and it keeps giving me the same error

    
asked by Eduardo Jesus 07.07.2016 в 06:25
source

2 answers

0

You are making the connection in the main thread. You have to pass that request to a secondary thread. For this you can use a AsyncTask (in this case this class will be contained within the class that carries your code)

class GetImageAsync extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        URL url = new URL(params[0]);
        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        return bmp;
    }

    @Override
    protected void onPostExecute(Bitmap bmp) {
       prueba2.setImageBitmap(bmp);
    }
}

Then you must modify your code, so that your ImageView test2 is a variable of the class to be able to access it from AsyncTask and you should create it like this:

  • If the version of Android is less than API 11: new GetImageAsync().execute("http://www.expertoanimal.com/es/images/9/7/5/img_nombres_para_perros_originales_y_bonitos_5579_paso_1_600.jpg");
  • If the version of Android is greater than or equal to API 11: new GetImageAsync().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "http://www.expertoanimal.com/es/images/9/7/5/img_nombres_para_perros_originales_y_bonitos_5579_paso_1_600.jpg");

In this link you have a lot more information about AsyncTask : link

Greetings.

    
answered by 07.07.2016 в 09:26
0

The error

  

NetworkOnMainThreadException : Caused when you try to perform   operations on the main thread (Main thread), which is incorrect.

It is related to a task being performed on the main thread, which is incorrect you can use a Asynctask or simply runOnUiThread ()

answered by 07.07.2016 в 12:40