Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 3.02 KB

from-gallery.md

File metadata and controls

66 lines (53 loc) · 3.02 KB

Selecting images from gallery / files.

Full sample code

class ImageActivity : AppCompatActivity() {

                                                    // Binding object to refer to xml.
                                                    // Not important.
    private lateinit var binding: ActivityImageBinding

                                                    // Modern alternative for onActivityResult().
                                                    // Cannot be lazy initialised.
                                                    // This is used to launch any gallery application,
                                                    // or the system file picker to select an image.
    private val imageSelectionLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()){

                                                    // If user selects an image, 
                                                    // then show it on Imageview.
            if (it.resultCode == Activity.RESULT_OK){
                it.data?.data?.let { uri ->
                    setImageFromUri(uri)
                }
            }
        }

                                                    // Function to show image on Imageview.
    private fun setImageFromUri(uri: Uri){
        binding.imageToShow.setImageURI(uri)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityImageBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.imageFromGalleryButton.setOnClickListener {
        
                                                    // For opening gallery, use intent action
                                                    // ACTION_PICK
                                                    // For uri, no visible difference was seen
                                                    // by using MediaStore.Images.Media.INTERNAL_CONTENT_URI
            val imageFromGalleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            imageSelectionLauncher.launch(Intent.createChooser(imageFromGalleryIntent, "Choose"))
        }
        
        binding.imageFromFilesButton.setOnClickListener {
        
                                                    // For selecting files, use intent action
                                                    // ACTION_OPEN_DOCUMENT
            val fileSelectionIntent = Intent(ACTION_OPEN_DOCUMENT).apply {
                addCategory(Intent.CATEGORY_OPENABLE)
                type = "image/*"
            }
            
                                                    // Another alternative is to use ACTION_GET_CONTENT
            //val fileSelectionIntent = Intent(ACTION_GET_CONTENT).apply {
            //  type = "image/*"
            //}
            
            imageSelectionLauncher.launch(Intent.createChooser(fileSelectionIntent, "Choose"))
        }
    }
}