How to Get Bitmap from a Url in Android

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();

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.

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
}

How to get bitmap of image from url

First step Sync gradle of Picasso to load image fro url.

implementation 'com.squareup.picasso:picasso:2.71828'

Second load image using picasso

String imgurl = objecturl.getString("imageurl");
Picasso.get().load(imgurl).into(imageView); //image will load successfully

Third use bitmap to convert image into bitmap

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();


Related Topics



Leave a reply



Submit