Unsplash Photo Picker for Android

repository·master·Indexed 18 days ago

https://github.com/unsplash/unsplash-photopicker-android

An Android UI component for integrating a high-quality photo search and selection interface from the Unsplash library. It provides the UnsplashPhotoPicker Kotlin object for initialization and UnsplashPickerActivity for launching the picker UI and retrieving selected UnsplashPhoto results.

Tokens
1.5K
Snippets
6
Records
8
Agent score
14%

What's inside unsplash-photopicker-android

  1. Install UnsplashPhotoPicker via Gradle

    master

    To integrate the Unsplash Photo Picker into your Android project, you must add the JitPack repository to your project-level build.gradle and then add the dependency to your app-module build.gradle.

    // Project-level build.gradle
    allprojects {
       repositories {
          ...
          maven { url  'https://jitpack.io' }
       }
    }
    
    // App-module build.gradle
    dependencies {
       implementation 'com.github.unsplash:unsplash-photopicker-android:x.y.z'
    }
  2. Initialize UnsplashPhotoPicker

    master

    The UnsplashPhotoPicker is a Kotlin object used to initialize the library. You should call the init method within your custom Application class's onCreate method. This requires your Unsplash developer credentials.

    UnsplashPhotoPicker.init(
                this, // application
                "your access key",
                "your secret key"
                /* optional page size */
            )
  3. Retrieve selected UnsplashPhoto results

    master

    After the user selects photos, retrieve the results in your calling activity's onActivityResult method. The selected photos are returned as an ArrayList<UnsplashPhoto> via the UnsplashPickerActivity.EXTRA_PHOTOS extra.

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
                val photos: ArrayList<UnsplashPhoto>? = data?.getParcelableArrayListExtra(UnsplashPickerActivity.EXTRA_PHOTOS)
                // use your photos here
            }
    }
  4. Present the UnsplashPickerActivity

    master

    To launch the photo picker, use UnsplashPickerActivity.getStartingIntent to create an Intent, then launch it using startActivityForResult. You can specify whether to allow multiple photo selections.

    startActivityForResult(
                    UnsplashPickerActivity.getStartingIntent(
                        this, // context
                        isMultipleSelection
                    ),
                    REQUEST_CODE
                )
  5. Launch the Unsplash Photo Picker

    master

    To present the Unsplash photo picker UI to the user, create an Intent using the UnsplashPickerActivity.getStartingIntent method. You can specify whether the user is allowed to select multiple photos by passing true or false to the isMultipleSelection parameter.

    When the user finishes selecting photos, the activity returns an Intent containing the selected photos in the EXTRA_PHOTOS extra.

    // Launching the picker for single selection
    val intent = UnsplashPickerActivity.getStartingIntent(context, false)
    startActivityForResult(intent, REQUEST_CODE)
    
    // Launching the picker for multiple selection
    val intent = UnsplashPickerActivity.getStartingIntent(context, true)
    startActivityForResult(intent, REQUEST_CODE)
    
    // Handling the result in onActivityResult
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            val selectedPhotos = data?.getParcelableArrayListExtra<UnsplashPhoto>(UnsplashPickerActivity.EXTRA_PHOTOS)
            // Use the selected photos
        }
    }
  6. Reference: UnsplashPickerActivity Intent Extras

    master

    The following keys are used in the Intent for UnsplashPickerActivity to configure its behavior or retrieve results.

    // Configuration (Input)
    EXTRA_IS_MULTIPLE // Used to enable/disable multiple photo selection
    
    // Results (Output)
    EXTRA_PHOTOS // Contains the ArrayList of selected UnsplashPhoto objects