Returning Bitmap from object URL
<p>I want to write a method that returns Bitmap image from object URL. But it gives <strong>NetworkOnMainThreadException</strong> error.</p>

<p>Image.java:</p>

<pre><code>package gc.x.models;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.annotations.SerializedName;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import gc.x.R;


/**
 * Created by ASUS on 4.10.2016.
 */
public class Image {


    public String albumId;
    public String id;
    public String title;
    public String url;
    public String thumbnailUrl;


    public Bitmap getBitmapFromURL() {//get bitmap of thumbnail from thumbnailurl
        try {
            URL url = new URL(this.thumbnailUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }


}
</code></pre>

<p>I don't want to simply show bitmap image from url because code structure is different.To prevent confusion, I did not share my all codes. I just want to write a method that gets bitmap from object's thumbnailurl. Later, I will use this method. I get images from <a href="http://jsonplaceholder.typicode.com/" rel="nofollow">http://jsonplaceholder.typicode.com/</a></p>