Skip to main content

How to add ImagePicker library dependency in Kotlin Android

How to add ImagePicker library dependency in Kotlin Android.

Here's a step-by-step tutorial on how to add the ImagePicker library dependency in Kotlin Android:

Step 1: Open your Android project in Android Studio.

Step 2: Open the app-level build.gradle file.

Step 3: Inside the dependencies block, add the following line to include the ImagePicker library:

implementation 'com.github.dhaval2404:imagepicker-support:1.4'

Step 4: Sync your project with the Gradle files by clicking on the "Sync Now" button in the toolbar.

Step 5: After syncing, you can start using the ImagePicker library in your Kotlin classes.

Step 6: To use the ImagePicker library, you need to add the necessary permissions to your AndroidManifest.xml file. Add the following lines inside the manifest tag:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 7: Create a new Kotlin class where you want to use the ImagePicker library. For example, let's create a class called "MainActivity".

Step 8: In the MainActivity class, import the necessary dependencies:

import com.github.dhaval2404.imagepicker.ImagePicker
import com.github.dhaval2404.imagepicker.util.IntentUtils

Step 9: Inside the MainActivity class, you can now use the ImagePicker library. For example, let's add a button and handle its click event to open the image picker:

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.dhaval2404.imagepicker.ImagePicker
import com.github.dhaval2404.imagepicker.util.IntentUtils

class MainActivity : AppCompatActivity() {

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

// Add a button click listener
button.setOnClickListener {
// Open the image picker
ImagePicker.with(this)
.crop()
.compress(1024)
.start()
}
}

// Handle the result of the image picker
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

// Get the selected image file
val file = ImagePicker.getFile(data)

// Do something with the selected image file
// For example, display it in an ImageView
imageView.setImageURI(file?.toUri())
}
}

That's it! You have successfully added and used the ImagePicker library in your Kotlin Android project. Now you can use the library to handle image picking and cropping in your application.