Select image from gallery using Kotlin

Viewed 21001

Recently i have started learning Kotlin. After having some basic functionality i am stuck with image picker.

Does there any specific way to select an image from gallery and camera using Kotlin? Or should i implement in our normal Java code and then call it from Kotlin file?

Java code :

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

Any other difference to perform this operation using Kotlin?

8 Answers

Now that startActivityForResult() is Deprecated, this is the new way. First, Do this inside onCreate

Kotlin:

    val selectImageIntent = registerForActivityResult(GetContent())
    { uri ->
        imageView.setImageURI(uri)
    }

Java :

ActivityResultLauncher<String> selectImageIntent = registerForActivityResult(
        new ActivityResultContracts.GetContent(),
        new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                imageView.setImageURI(uri);
            }
        });

And call selectImageIntent.launch("image/*") to launch gallery.

You can try the following:

val galleryIntent = Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent, requestcode)
fun Fragment.openGalleryForPickingImage(code: Int) {
    Intent().apply {
        type = "image/*"
        action = Intent.ACTION_GET_CONTENT
        startActivityForResult(Intent.createChooser(this, getString(R.string.select_file)), code)
    }
}

The problem with it, is that the function to get real path doesn't work because content resolver doesn't find the columnIndex corresponding to MediaStore.Images.Media.DATA Instead, you need to do:

fun Fragment.openGalleryForPickingImage(code: Int) {
    Intent(
        Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    ).apply {
        startActivityForResult(Intent.createChooser(this, getString(R.string.select_file)), code)
    }
}

kotlin :

fun getPhoto() {
    val intent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
    startActivityForResult(intent,1)
}

you can follow the tutorial https://www.viralpatel.net/pick-image-from-galary-android-app/

inside onActivityResult add this code

if(requestCode == PICK_IMAGE && data != null){
            val selectedImage = data?.data
            val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)

            val cursor =
                contentResolver.query(selectedImage!!, filePathColumn, null, null, null)!!
            cursor.moveToFirst()

            val columnIndex = cursor.getColumnIndex(filePathColumn[0])
            mediaPath = cursor.getString(columnIndex)
            var f = File(mediaPath)
            var filename = f.getName()
            mediaPathArray!!.add(mediaPath)
            Log.d("filename", filename)
            Log.d("imgpath", mediaPath)
            image.setImageBitmap(BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage)))
            cursor.close()
        }
Related