I've been trying to download an image into a bitmap element to load them into a gridView with images, but I need to do it async because if not gives the error "android.os.NetworkOnMainThreadException".
Searching I found a lot of tutorials to do that, but all of them add this element to an ImageView when finished. Is there any way to do that but into a Bitmap?
Here I have ways to download without async that I found:
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
One more:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}