Ich möchte eine Methode schreiben, die Bitmap-Bild von Objekt-URL zurückgibt. Aber es gibt NetworkOnMainThreadException Fehler.Bitmap aus Objekt-URL zurückgeben
Image.java:
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;
}
}
}
Ich will nicht einfach aus url Bitmap-Bild zeigen, weil Codestruktur Verwirrung different.To zu verhindern ist, habe ich nicht meine alle Codes teilen. Ich möchte nur eine Methode schreiben, die Bitmap aus der Thumbnailurl des Objekts bekommt. Später werde ich diese Methode verwenden. Ich bekomme Bilder von http://jsonplaceholder.typicode.com/
Sie können nicht Netzwerk-Aufruf auf Haupt-Thread tun, falls Sie Bild laden möchten gibt es mehrere Bild-Loader-Bibliothek in Android, zum Beispiel: Glide https://github.com/bumptech/glide – hakim