Android Load from Url to Bitmap

How to get bitmap from a url in android?

This is a simple one line way to do it:

    try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}

Android load from URL to Bitmap


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) {
// Log exception
return null;
}
}

Convert url to bitmap in NetworkOnMainThreadException

simply do as below -

public class MyAsync extends AsyncTask<Void, Void, Bitmap>{

@Override
protected Bitmap doInBackground(Void... params) {

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;
}

}
}

Now to access the bitmap from url do as below -

MyAsync obj = new MyAsync(){

@Override
protected void onPostExecute(Bitmap bmp) {
super.onPostExecute(bmp);

Bitmap bm = bmp;
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new AbsListView.LayoutParams(250, 250));
layout.setGravity(Gravity.CENTER);

ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new AbsListView.LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bm);

layout.addView(imageView);
}
};

and then finally execute the AsynTask -

obj.execute();

How to get bitmap from URL using Coil?

You can use target method and cast the drawable to bitmap as

    val loader = ImageLoader(this)
val req = ImageRequest.Builder(this)
.data("https://images.dog.ceo/breeds/saluki/n02091831_3400.jpg") // demo link
.target { result ->
val bitmap = (result as BitmapDrawable).bitmap
}
.build()

val disposable = loader.enqueue(req)

If you using coroutines then use GetRequest (with overloaded execute method with suspend) in your CoroutineScope as:

  coroutineScope.launch{
val loader = ImageLoader(this)
val request = ImageRequest.Builder(this)
.data("https://images.dog.ceo/breeds/saluki/n02091831_3400.jpg")
.allowHardware(false) // Disable hardware bitmaps.
.build()

val result = (loader.execute(request) as SuccessResult).drawable
val bitmap = (result as BitmapDrawable).bitmap
}

Cannot convert url to bitmap in Android

You are trying to run network-consuming task in your main thread (what may prevent UI from being refreshed - that's why Android is complaining).

You should start an AsyncTask which you can use to download your image and return in dedicated callback method.

For cases like this, personally, I am using Picasso library. With this tool you can simplify your issue to just few steps as described here.



Related Topics



Leave a reply



Submit