'Select image from gallery using Kotlin
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
?
Solution 1:[1]
Here a sample function code for selecting image and capture image:
fun selectImageInAlbum() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, REQUEST_SELECT_IMAGE_IN_ALBUM)
}
}
fun takePhoto() {
val intent1 = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (intent1.resolveActivity(packageManager) != null) {
startActivityForResult(intent1, REQUEST_TAKE_PHOTO)
}
}
companion object {
private val REQUEST_TAKE_PHOTO = 0
private val REQUEST_SELECT_IMAGE_IN_ALBUM = 1
}
Also don't forget to add this to your manifest file:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I hope i can help
Solution 2:[2]
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
}
Solution 3:[3]
You can write it at Kotlin too.
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
Solution 4:[4]
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.
Solution 5:[5]
You can try the following:
val galleryIntent = Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent, requestcode)
Solution 6:[6]
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)
}
}
Solution 7:[7]
kotlin :
fun getPhoto() {
val intent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,1)
}
Solution 8:[8]
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()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Melchizedek |
Solution 2 | shinilms |
Solution 3 | Egor tepikin |
Solution 4 | Fine Boy |
Solution 5 | madlymad |
Solution 6 | Barbara K |
Solution 7 | SimonC |
Solution 8 |