readthefuckingmanual.net

[SOLVED] android.os.NetworkOnMainThreadException

Error added: 2017-03-11T15:01:52Z

0 people waiting for the answer...

1 answers found.

Answer 2477 (100.0% helpful)

public class LoadPicTask extends AsyncTask<String, Void, Bitmap> {

    private final ImageView imageView;

    public LoadPicTask(ImageView imageView) {
        this.imageView = imageView;
    }

    protected Bitmap doInBackground(String... urls) {
        try {
            URL url = new URL("https://blah.whatever/url/thing");
            return BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        // do what you want with it
    }
}
Permalink

Add an answer/solution

If you know the answer, please add your own solution below.
If you don't know, but find out later, please come back and share your answer - there will be other people struggling with this too.


Please enter 61948 here

If you want to be notified via email when this is solved, enter your email address here: