skip to Main Content

im triying to use an image picker so I can open the gallery and choose a pic for the user profile.
Apparently the built-in image picker works only on Android 13 (API 33) so in order to add this feature i have to add this library.

The first step is not clear to me:

Gradle dependency:

allprojects {
   repositories {
        maven { url "https://jitpack.io" }
   }
}
implementation 'com.github.dhaval2404:imagepicker:2.1'

If you are yet to Migrate on AndroidX, Use support build artifact:

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

I added the second section code in build.gradle (App level) in order to make it work but when i try to implement it in my code I get this message

Adding the dependency suggested by Android Studio does not solve the problem, 99% I’m missing something.

2

Answers


  1. When you add a new library reference in your app/bundle.gradle file:

    dependencies {
     implementation 'com.github.dhaval2404:imagepicker-support:1.7.1'
    // ..
    }
    

    be sure to run Gradle sync:

    Gradle sync button

    Then to need to add import in the java/kotlin source code:

    import com.github.dhaval2404.imagepicker.ImagePicker
    

    just like in the sample of that library

    Login or Signup to reply.
  2. Just in case you didn’t know, you can already open a file picker that only displays photos – the Photo Picker thing is a new feature in Android 13 (which isn’t even released yet), so this is still the standard way to do it!

    const val REQUEST_IMAGE_OPEN = 1
    
    fun selectImage() {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
            type = "image/*"
            addCategory(Intent.CATEGORY_OPENABLE)
        }
        // opens the standard document browser, filtered to images
        startActivityForResult(intent, REQUEST_IMAGE_OPEN)
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        if (requestCode == REQUEST_IMAGE_OPEN && resultCode == Activity.RESULT_OK) {
            val fullPhotoUri: Uri = data.data
            // Do work with full size photo saved at fullPhotoUri
        }
    }
    

    There’s actually two versions at the link – one uses GET_CONTENT which copies the image, the other uses OPEN_DOCUMENT which gives you a URI pointing to the existing file on disk. Read the descriptions to see which works best for whatever you’re doing.

    So yeah, if you want something that looks like a plain photo gallery, you’ll either need the new picker or a library that does similar. If you just want to let the user load an image though, the standard document browser should be familiar

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search