androidkotlinandroid-studio

Change the image of an ImageView by code in Android Studio


I'm trying to get an ImageView by code It's declared that way in my XML:

<ImageView
    android:id="@+id/exo_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@color/cardview_light_background"/>

And I'm setting it by code that way:

val bmp = BitmapFactory.decodeFile(mediaItem!!.mediaMetadata.artworkUri!!.path)
view.setImageBitmap(null)
if (bmp != null) {
    view.setImageBitmap(bmp)
    Log.d("TEMPS", bmp.toString())
}

My URL point to a local file saved in my application data asking to the following: /data/user/0/com.example.euphonia/files/music.zirk.eu/icon/Howling.jpg

My log returns me the following value: android.graphics.Bitmap@f241913

I also tried to set it directly from the URL but it didn't work

To check if my file was incorrect I also tried to set the bitmap the following way:

val help = Bitmap.createBitmap(100, 100, Config.ARGB_8888)
for (i in 0..99) {
    for (y in 0..99) {
        help.setPixel(i, y, Color.rgb(45, 127, 0))
    }
}
view.setImageBitmap(help)

But even with that my file is still not displayed I also tried to invalidate the view by calling invalidate but that didn't change anything

How can I display an image in an ImageView?


Solution

  • Make sure you're setting the image to the correct ID. In your case, "exo_image". You're using view.setImageBitmap(bmp). Make sure "view" is referring to "exo_image"