PictureSelector

repository·version_component·Indexed 12 days ago

https://github.com/lucksiege/pictureselector

An Android library for selecting pictures, videos, audio, and photos from albums, supporting Android 5.0 and above. Version 3.11.2 includes features for cropping, compression, theme customization, and camera capture. It provides flexible integration options via GlideEngine, PicassoEngine, or CoilEngine, and supports both Java and Kotlin projects.

Tokens
7.7K
Snippets
21
Records
23
Agent score
46%

What's inside PictureSelector

  1. Configure ImageEngine for PictureSelector

    version_component

    PictureSelector allows you to use different image loading engines to handle image display. The library provides implementations for common engines. You can choose the one that matches your project's existing image loading stack:

    • GlideEngine: Uses Glide
    • PicassoEngine: Uses Picasso
    • CoilEngine: Uses Coil
  2. Install PictureSelector via Maven

    version_component

    You can also include PictureSelector and its optional modules using Maven coordinates.

    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>pictureselector</artifactId>
      <version>v3.11.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>compress</artifactId>
      <version>v3.11.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>ucrop</artifactId>
      <version>v3.11.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>camerax</artifactId>
      <version>v3.11.2</version>
    </dependency>
  3. Install PictureSelector for Kotlin projects

    version_component

    If you are using a Kotlin-based project, use the specific Kotlin version of the library. It is recommended to check the official Kotlin Demo before upgrading to avoid version mismatches.

    dependencies {
      // Please do not upgrade across versions; check the Kotlin Demo first
      implementation 'io.github.lucksiege:pictureselector:kotlin-v1.0.0-beta'
    }
  4. Install PictureSelector via Gradle

    version_component

    To use PictureSelector in your Android project, add the following dependencies to your build.gradle file. Note that pictureselector is the core required dependency, while compress, ucrop, and camerax are optional components for specific features like compression, cropping, and custom camera support.

    repositories {
      google()
      mavenCentral()
    }
    
    dependencies {
      // PictureSelector Core (Required)
      implementation 'io.github.lucksiege:pictureselector:v3.11.2'
    
      // Image Compression (Optional)
      implementation 'io.github.lucksiege:compress:v3.11.2'
    
      // Image Cropping (Optional)
      implementation 'io.github.lucksiege:ucrop:v3.11.2'
    
      // Custom Camera (Optional)
      implementation 'io.github.lucksiege:camerax:v3.11.2'
    }
  5. ProGuard Configuration

    version_component

    To prevent code shrinking from breaking PictureSelector, add the following rules to your ProGuard configuration. Include the Camerax and uCrop rules if you are using those specific features.

    -keep class com.luck.picture.lib.** { *; }
    
    // use Camerax
    -keep class com.luck.lib.camerax.** { *; }
    
    // use uCrop
    -dontwarn com.yalantis.ucrop**
    -keep class com.yalantis.ucrop** { *; }
    -keep interface com.yalantis.ucrop** { *; }
  6. Configure Android Permissions for PictureSelector

    version_component

    PictureSelector requires several permissions in your AndroidManifest.xml to access storage, camera, and media.

    Standard Permissions

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />

    Android 13+ Compatibility

    For Android 13 and above, use the granular media permissions:

    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

    Android 11 Camera Queries

    If you are using the camera on Android 11, you must add the following <queries> block to your AndroidManifest.xml to allow intent resolution:

    <queries package="${applicationId}">
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
        <intent>
            <action android:name="android.media.action.ACTION_VIDEO_CAPTURE" />
        </intent>
    </queries>
  7. Install PictureSelector via Maven

    version_component

    For Maven-based projects, add the following dependencies to your pom.xml.

    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>pictureselector</artifactId>
      <version>v3.11.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>compress</artifactId>
      <version>v3.11.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>ucrop</artifactId>
      <version>v3.11.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.github.lucksiege</groupId>
      <artifactId>camerax</artifactId>
      <version>v3.11.2</version>
    </dependency>
  8. Select images from the gallery

    version_component

    Use openGallery() to allow users to select media from the local gallery. You must specify the media type using SelectMimeType and provide an ImageEngine (e.g., GlideEngine) to handle image loading. Results are returned via OnResultCallbackListener<LocalMedia>.

    PictureSelector.create(this)
       .openGallery(SelectMimeType.ofImage())
       .setImageEngine(GlideEngine.createGlideEngine())
       .forResult(new OnResultCallbackListener<LocalMedia>() {
          @Override
          public void onResult(ArrayList<LocalMedia> result) {
    
          }
    
          @Override
          public void onCancel() {
    
          }
    });
  9. Use the system photo picker

    version_component

    Use openSystemGallery() to invoke the native Android system photo picker instead of the PictureSelector custom UI.

    PictureSelector.create(this)
         .openSystemGallery(SelectMimeType.ofImage())
         .forResult(new OnResultCallbackListener<LocalMedia>() {
            @Override
            public void onResult(ArrayList<LocalMedia> result) {
    
            }
    
            @Override
            public void onCancel() {
    
            }
    });
  10. Customize UI and Layout

    version_component

    Customize the selector's appearance using setSelectorUIStyle() or provide a custom layout by implementing setInjectLayoutResourceListener().

    // Set predefined UI style
    .setSelectorUIStyle();
    
    // Custom layout injection
    .setInjectLayoutResourceListener(new OnInjectLayoutResourceListener() {
       @Override
       public int getLayoutResourceId(Context context, int resourceSource) {
    	return 0;
       }
    });
  11. Inject PictureSelector into a Fragment or View

    version_component

    You can launch the selector by injecting it into a specific container ID using buildLaunch(), or manually manage the fragment transaction using build().

    // Option 1: Automatic injection into a container
    PictureSelector.create(this)
       .openGallery(SelectMimeType.ofAll())
       .setImageEngine(GlideEngine.createGlideEngine())
       .buildLaunch(R.id.fragment_container, new OnResultCallbackListener<LocalMedia>() {
          @Override
          public void onResult(ArrayList<LocalMedia> result) {
          
          }
    
          @Override
          public void onCancel() {
          
          }
    });
    
    // Option 2: Manual Fragment transaction
    PictureSelectorFragment selectorFragment = PictureSelector.create(this)
         .openGallery(SelectMimeType.ofAll())
         .setImageEngine(GlideEngine.createGlideEngine())
         .build();
         
    getSupportFragmentManager().beginTransaction()
         .add(R.id.fragment_container, selectorFragment, selectorFragment.getFragmentTag())
         .addToBackStack(selectorFragment.getFragmentTag())
         .commitAllowingStateLoss();
  12. Query media data and album lists

    version_component

    You can retrieve data sources directly without launching the UI. Use obtainAlbumData() for folder lists, obtainMediaData() for media items, or buildMediaLoader() for more granular control via IBridgeMediaLoader.

    // Get Album List
    PictureSelector.create(this)
        .dataSource(SelectMimeType.ofAll())
        .obtainAlbumData(new OnQueryDataSourceListener<LocalMediaFolder>() {
            @Override
            public void onComplete(List<LocalMediaFolder> result) {
    
            }
       );
    
    // Get Media List
    PictureSelector.create(this)
        .dataSource(SelectMimeType.ofAll())
        .obtainMediaData(new OnQueryDataSourceListener<LocalMedia>() {
            @Override
            public void onComplete(List<LocalMedia> result) {
    
            }
       });
    
    // Using IBridgeMediaLoader
    IBridgeMediaLoader loader = PictureSelector.create(this)
        .dataSource(SelectMimeType.ofImage()).buildMediaLoader();
    loader.loadAllAlbum(new OnQueryAllAlbumListener<LocalMediaFolder>() {
        @Override
        public void onComplete(List<LocalMediaFolder> result) {
    
        }
      });