GalleryPick

repository·master·Indexed 19 days ago

https://github.com/yancyye/gallerypick

An Android library for custom photo albums that supports camera access, single or multiple image selection, cropping, and rotation. It is designed to be uncoupled from specific image loading frameworks, requiring an implementation of the ImageLoader interface (e.g., Glide, Picasso, or Fresco). The library provides a Builder pattern via GalleryConfig for customization and uses IHandlerCallBack to handle selection results.

Tokens
3K
Snippets
7
Records
8
Agent score
16%

What's inside GalleryPick

  1. Open the camera directly

    master

    There are three ways to trigger the camera immediately instead of opening the gallery:

    1. Via GalleryConfig: Set .isOpenCamera(true) during configuration.
    2. Via Builder modification: If you already have a GalleryConfig instance, use .getBuilder().isOpenCamera(true).build() to create a modified version.
    3. Via direct method call: Use the convenience method openCamera(mActivity) on the GalleryPick instance. This is the simplest method as it does not require modifying the existing GalleryConfig.
    // Method 1: In Builder
    GalleryConfig galleryConfig = new GalleryConfig.Builder()
              .iHandlerCallBack(iHandlerCallBack)
              .isOpenCamera(true)
              .build();
    GalleryPick.getInstance().setGalleryConfig(galleryConfig).open(mActivity);
    
    // Method 2: Modify existing config
    galleryConfig.getBuilder().isOpenCamera(true).build();
    GalleryPick.getInstance().setGalleryConfig(galleryConfig).open(mActivity);
    
    // Method 3: Direct call (Recommended for convenience)
    GalleryPick.getInstance().setGalleryConfig(galleryConfig).openCamera(mActivity);
  2. Enable and configure the cropping feature

    master

    The cropping feature is available via GalleryConfig.Builder.

    Important Constraints:

    • Cropping only works when in single-selection mode or when opening the camera directly.
    • Cropped images are stored in a crop subdirectory within the directory specified by filePath.
    • The library automatically creates a .nomedia file in the crop directory to prevent cropped images from appearing in the system gallery.

    To use the default 1:1 aspect ratio, use .crop(true). To specify a custom aspect ratio and dimensions, use .crop(true, ratioX, ratioY, width, height).

    // Default 1:1 cropping
    GalleryConfig galleryConfig = new GalleryConfig.Builder()
                    .imageLoader(new GlideImageLoader())
                    .iHandlerCallBack(iHandlerCallBack)
                    .provider("com.yancy.gallerypickdemo.fileprovider")
                    .crop(true)
                    .build();
    
    // Custom aspect ratio and dimensions
    GalleryConfig galleryConfigCustom = new GalleryConfig.Builder()
                    .imageLoader(new GlideImageLoader())
                    .iHandlerCallBack(iHandlerCallBack)
                    .provider("com.yancy.gallerypickdemo.fileprovider")
                    .crop(true, 1, 1, 500, 500) // ratio 1:1, 500x500
                    .build();
    
    GalleryPick.getInstance().setGalleryConfig(galleryConfig).open(mActivity);
  3. Configure FileProvider for GalleryPick

    master

    To support file sharing and camera functionality, you must set up a FileProvider in your AndroidManifest.xml and define paths in an XML resource file.

    1. Update AndroidManifest.xml

    Add a <provider> tag inside the <application> element. The android:authorities value must be unique (typically your package name + the provider name).

    2. Create paths XML

    Create an XML file (e.g., res/xml/filepaths.xml) to define accessible paths:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <paths>
            <external-path
                name="external"
                path="" />
            <files-path
                name="files"
                path="" />
            <cache-path
                name="cache"
                path="" />
        </paths>
    </resources>

    When building your GalleryConfig, pass the exact string used in android:authorities to the .provider() method.

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.yancy.gallerypickdemo.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>
  4. Install GalleryPick via Gradle or Maven

    master

    GalleryPick is hosted on JitPack. You can integrate it into your Android project using either Gradle or Maven.

    Using Gradle

    1. Add the JitPack repository to your project-level build.gradle:
    allprojects {
    	repositories {
    		...
    		maven { url "https://jitpack.io" }
    	}
    }
    1. Add the dependency to your app-level build.gradle:
    dependencies {
          compile 'com.github.YancyYe:GalleryPick:1.2.1'
    }

    Using Maven

    Add the JitPack repository and the dependency to your pom.xml:

    <repositories>
    	<repository>
    	    <id>jitpack.io</id>
    	    <url>https://jitpack.io</url>
    	</repository>
    </repositories>
    
    <dependency>
        <groupId>com.github.YancyYe</groupId>
        <artifactId>GalleryPick</artifactId>
        <version>1.2.1</version>
    </dependency>
    dependencies {
          compile 'com.github.YancyYe:GalleryPick:1.2.1'
    }
  5. Customize the UI via resource overriding

    master

    You can customize the appearance of GalleryPick by overriding its internal resource files in your own app's res directory.

    Changing Colors

    To change a color (e.g., the title bar color), define a color resource in your colors.xml using the same name used in the library. For example, if the library uses @color/gallery_blue, add this to your res/values/colors.xml:

    <resources>
        <color name="gallery_blue">#FF4081</color>
    </resources>

    Changing Layouts (Text, Icons, etc.)

    To change more complex elements like text color or icons:

    1. Locate the library's layout file (e.g., gallery_title.xml).
    2. Copy the content of that file into your own app's res/layout/gallery_title.xml.
    3. Modify the attributes (e.g., change android:textColor or android:src for an ImageView) within your copied file.
  6. Implement IHandlerCallBack for selection results

    master

    Implement the IHandlerCallBack interface to handle the lifecycle and results of the image selection process. This interface provides callbacks for starting, succeeding, canceling, finishing, or encountering errors.

    IHandlerCallBack iHandlerCallBack = new IHandlerCallBack() {
               @Override
               public void onStart() {
                   Log.i(TAG, "onStart: 开启");
               }
    
               @Override
               public void onSuccess(List<String> photoList) {
                   Log.i(TAG, "onSuccess: 返回数据");
                   for (String s : photoList) {
                       Log.i(TAG, s);
                   }
               }
    
               @Override
               public void onCancel() {
                   Log.i(TAG, "onCancel: 取消");
               }
    
               @Override
               public void onFinish() {
                   Log.i(TAG, "onFinish: 结束");
               }
    
               @Override
               public void onError() {
                   Log.i(TAG, "onError: 出错");
                }
    };
  7. Configure GalleryConfig using the Builder pattern

    master

    Use GalleryConfig.Builder to customize the behavior of the image picker.

    Required Fields:

    • .imageLoader(ImageLoader): An implementation of the ImageLoader interface (e.g., using Glide, Picasso, or Fresco).
    • .iHandlerCallBack(IHandlerCallBack): Your implementation of the result listener.
    • .provider(String): The authorities string defined in your FileProvider setup.

    Optional Fields (with defaults):

    • .pathList(List<String>): Pre-selected image paths.
    • .multiSelect(boolean): Enables multiple selection (default: false).
    • .multiSelect(boolean, int): Enables multiple selection with a specific limit.
    • .maxSize(int): Maximum number of items for multi-selection (default: 9).
    • .crop(boolean): Enables cropping (only valid for single selection or camera mode).
    • .crop(boolean, int, int, int, int): Configures crop parameters (default ratio 1:1).
    • .isShowCamera(boolean): Shows/hides the camera button (default: false).
    • .filePath(String): Destination path for images (default: /Gallery/Pictures).
    GalleryConfig galleryConfig = new GalleryConfig.Builder()
                    .imageLoader(new GlideImageLoader())    // Required
                    .iHandlerCallBack(iHandlerCallBack)     // Required
                    .provider("com.yancy.gallerypickdemo.fileprovider")   // Required
                    .pathList(path)                         // Optional
                    .multiSelect(false)                      // Optional
                    .multiSelect(false, 9)                   // Optional
                    .maxSize(9)                             // Optional
                    .crop(false)                             // Optional
                    .crop(false, 1, 1, 500, 500)             // Optional
                    .isShowCamera(true)                     // Optional
                    .filePath("/Gallery/Pictures")          // Optional
                    .build();