Leading by example, @Joseinnewworld joined the wave and lit up the scene with 9 #NFTs sparks ⚡🔥 A true leader in the collector community — thank you for keeping the energy alive 🙌 #eCash $XEC #NFTCommunity #nftprimearts #NFTCollectors #NFTdrop #NFTartist #CryptoMarket pic.twitter.com/GrKhIlETCf
— NFToa (@nftoa_) September 3, 2025
The first thing we need to know is, if the image source is outside the Android Studio project assets, such as the image URL for example, then we need to make an HTTP request.
On this occasion I will share how to load images in ImageView using HttpURLconnection & Glide.
1. HttpURLconnection
The first method is short but not reusable
val appImgUrlLink = URL("your_img_url")
val avatar: Bitmap = BitmapFactory.decodeStream(appImgUrlLink.openConnection().getInputStream())The second way is that we create a function that can be accessed globally, so that we can call it in any class.
fun getBitmapFromURL(strURL: String?): Bitmap? {
try {
val url = URL(strURL)
val connection = url.openConnection() as HttpURLConnection
connection.setDoInput(true)
connection.connect()
val input = connection.getInputStream()
return BitmapFactory.decodeStream(input)
} catch (e: IOException) {
e.printStackTrace()
return null
}
}Then, when a different class needs to call the above function, we can declare the variable first as a bitmap, then we initialize it in onCreate()
YourClass: AppCompatActivity{
//deklarasi variabel
private lateinit var myImage: Bitmap
override onCreate{
//lalu inisialisasi variabel tersebut
myImage = getBitmapFromURL("your_img_url")
}
}Or it can also be implemented directly in onCreate()
2.
override onCreate{
//lalu inisialisasi variabel tersebut
val myImage = getBitmapFromURL("your_img_url")
}2. Glide
Glide is an open-source, fast & efficient image media management, image loading framework for Android that combines media decoding, memory and disk caching, and resource pooling into a simple, easy-to-use interface.
val avatar: Bitmap = Glide.with(this@MyFirebaseMessagingService).asBitmap().load("your_img_url").submit().get()Okay, that's it guys, good luck!
