0

I have an url passed to an activity and I am trying to show the image from the url full screen, however it throws a main network thread exception.

From what I can find I believe I have to put the method in an async task however I cannot seem to make sense of it at all. So how would I put this method in an async task?

FullScreenImageView.java

public class FullscreenImageView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String url = getIntent().getStringExtra("SelectedImageURL");



    try {
        ImageView i = (ImageView)findViewById(R.id.imgView);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}
numaroth
  • 1,295
  • 4
  • 25
  • 36
  • 1
    put `BitmapFactory.decodeStream` into doInBackground return Bitmap and in onPostExecute set this bitmap to image – Selvin Sep 17 '14 at 13:22
  • http://www.learn2crack.com/2014/06/android-load-image-from-internet.html – Naveen Tamrakar Sep 17 '14 at 13:22
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – panini Sep 19 '14 at 04:39

3 Answers3

2

It should be something like this. In the doInBackground you get the image, and in the onPostExecute you set it

private class DownloadFilesTask extends AsyncTask<String, Void, Bitmap>  {
     @Override
     protected Bitmap doInBackground(String... urls) {
          Bitmap bitmap = null; 
          try {
              bitmap = BitmapFactory.decodeStream((InputStream)new URL(urls[0]).getContent());
          } catch (MalformedURLException e) {
               e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
          return bitmap;
     }
     @Override
     protected void onPostExecute(Bitmap bitmap) {
        ImageView i = (ImageView)findViewById(R.id.imgView);
        i.setImageBitmap(bitmap);
     }
 }

Then, you call it inside your onCreate method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = getIntent().getStringExtra("SelectedImageURL");
    new DownloadFilesTask ().execute(url); 
}
Selvin
  • 6,598
  • 3
  • 37
  • 43
arlistan
  • 731
  • 9
  • 20
0

Put your network task inside doInBackground() method of AsyncTask as follow:

@Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

try {
        //ImageView i = (ImageView)findViewById(R.id.imgView);Put this line in onPreExecute() method
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
       // i.setImageBitmap(bitmap);//and this one on postExecute() method
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
Nabin
  • 11,216
  • 8
  • 63
  • 98
0

Using Picasso after initializing yourImageView with findViewById();

Picasso.with(context).load(url).into(yourImageView)
David Corsalini
  • 7,958
  • 8
  • 41
  • 66