Trying to build a program that enables users to select image from gallery but when I run and choose the image the image doesn't appear in the app

Viewed 394

Trying to build a program that enables users to select image from gallery but when I run and choose the image the image doesn't appear in the app

I have been trying to fix this but I find no problem with the codes.

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20sp"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/significant"
        android:layout_width="270dp"
        android:layout_height="480dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="@+id/significant"
        app:layout_constraintStart_toStartOf="@+id/significant"
        app:layout_constraintTop_toBottomOf="@+id/significant" />


</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin class

package com.example.projectc

import android.app.Activity
import android.content.Intent
import android.media.Image
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        button.setOnClickListener {
            Intent(Intent.ACTION_GET_CONTENT).also {
                it.type = "image/*"
                startActivityForResult(it, 0)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Activity.RESULT_OK && resultCode == 0) {
            val uri = data?.data
            significant.setImageURI(uri)

        }
    }
}
2 Answers
if (requestCode == Activity.RESULT_OK && resultCode == 0)

requestCode is the parameter that you passed to startActivityForResult(). Try:

if (requestCode == 0 && resultCode == Activity.RESULT_OK)

And, eventually, look to switch to the ActivityResultContracts.GetContent or ActivityResultsContracts.OpenDocument APIs instead of using ACTION_GET_CONTENT directly.

According to the documentation you can easily pick an image from the gallery by these steps

1- declare this variable in your activity

val getContent = registerForActivityResult(GetContent()) { uri: Uri? ->
   uri?.apply{
      // put the selected image in your ImageView
      significant.setImageURI(this)
   }
}

2- when user click on the button just call this line

getContent.launch("image/*")
Related