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();
}
}
}
It should be something like this. In the
doInBackground
you get the image, and in theonPostExecute
you set itThen, you call it inside your
onCreate
methodPut your network task inside
doInBackground()
method of AsyncTask as follow:Using Picasso after initializing
yourImageView
with findViewById();