skip to Main Content

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?

2

Answers


  1. 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"

    Login or Signup to reply.
  2. To do this, I use the picasso library in my app

    in build.gradle(app)

        dependencies {
    // other
        implementation 'com.squareup.picasso:picasso:2.8'
        //
        implementation 'com.squareup.okhttp3:okhttp:4.11.0'
    // other
    }
    

    in a helper class

    /**
     * inserts an image from url into a view
     * Refer to [picasso](https://square.github.io/picasso/#license)
     *
     * @param url string with url of the image
     * @param img view with target view
     * @param width int with the image width for resizing
     * @param height int with the image height for resizing
     * @see com.squareup.picasso.Picasso
     */
    @JvmStatic
    fun addImageUsingPicasso(url: String?, img: ImageView?, width: Int, height: Int) {
        Picasso.get()
            .load(url)
            .resize(width, height)
            .into(img)
    }
    
    /**
     * inserts an image from file into a view
     * Refer to [picasso](https://square.github.io/picasso/#license)
     *
     * @param file file containing the image
     * @param img view with target view
     * @param width int with the image width for resizing
     * @param height int with the image height for resizing
     * @see com.squareup.picasso.Picasso
     */
    @JvmStatic
    fun addFileImageUsingPicasso(file: File?, img: ImageView?, width: Int, height: Int) {
        Picasso.get()
            .load(file!!)
            .resize(width, height)
            .into(img)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search