0

I trying read a external imagen but I've the error

"android.os.NetworkOnMainThreadException" and "http.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)"

OnCreate

imageView = (ImageView) findViewById(R.id.image_view);
downloadFile(imageHttpAddress);

Function downloadFile

void downloadFile(String imageHttpAddress) {
    URL imageUrl = null;
    try {
        imageUrl = new URL(imageHttpAddress);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.connect();
        loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
        imageView.setImageBitmap(loadedImage);
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Error cargando la imagen: "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Jordi Salom
  • 121
  • 10
  • Possible duplicate of [this question](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception). – Glorfindel Jul 25 '15 at 16:25
  • do this downloading thing on different thread. Use [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) instead. – Ratul Sharker Jul 25 '15 at 16:27

2 Answers2

0

You can use this function ;)

public Bitmap DownloadImage(String STRURL) {
Bitmap bitmap = null;
InputStream in = null;       
try {
    int response = -1;
    URL url = new URL(STRURL);
    URLConnection conn = url.openConnection();
    if (!(conn instanceof HttpURLConnection))             
    throw new IOException("Not an HTTP connection");
    try{
    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestMethod("GET");
    httpConn.connect();
    response = httpConn.getResponseCode();  
    if (response == HttpURLConnection.HTTP_OK) {
    in = httpConn.getInputStream();
    }                    
    }catch(Exception ex) {
    throw new IOException("Error connecting"); 
    }
    bitmap = BitmapFactory.decodeStream(in);
    in.close();
    }catch (IOException e1) {
    e1.printStackTrace();
    }
    return bitmap
}
HAMIDREZA
  • 1
  • 2
0

From Android docs

You should not perform network operations on the UI thread.

To avoid this exception, you can use AsyncTask for your request.

    imageView = (ImageView) findViewById(R.id.image_view);

        new AsyncTask<String, Void, Bitmap>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected Bitmap doInBackground(String... url) {
                return downloadFile(url[0]);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                imageView.setImageBitmap(bitmap);
            }
        }.execute(imageHttpAddress);

        private Bitmap downloadFile(String imageHttpAddress) {
            URL imageUrl = null;
            try {
                imageUrl = new URL(imageHttpAddress);
                HttpURLConnection conn =(HttpURLConnection)imageUrl.openConnection(); 
                conn.connect();
                loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
                return loadedImage; 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Even better, use some Async image handling library for android. there are many of these around the internet. A very popular one is picasso