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
Java -Xbootclass Path Is No Longer a Supported Option
How Does a Jvm Process Allocate Its Memory
Find Native Memory Leak in Java Application Using Jemalloc
Execute Source Command from Java
Exception While Submitting a Mapreduce Job from Remote System
In Laymans Terms, What Does 'Static' Mean in Java
Changing Edittext Bottom Line Color with Appcompat V7
What Is Contained in Code/Internal Sections of Jcmd
Seeing All the System Calls That Were Made by a Java Program
Why Does Java Rmi Keep Connecting to 127.0.1.1. When Ip Is 192.168.X.X
Verify If a Process Is Running Using Its Pid in Java
Too Many Open Files Error But Lsof Shows a Legal Number of Open Files
Reading Remote Hdfs File with Java
Execute Bash Command Within Java Program
How to Read All Network Cards with Java
Converting Java Bitmap to Byte Array
In Linux, How to Execute Java Jar File with External Jar Files