Integrating a Customizable Android Photo Gallery: GalleryFinal Guide
Overview
The GalleryFinal library provides a flexible Android gallery solution that handles photo capture, selection (single/multi), cropping, rotation, and editing. It avoids common issues like upside-down photos, device compatibility crashes, and limited system gallery functionality. It supports popular image loaders including Glide, Fresco, Picasso, and Universal Image Loader.
Note: The library is compiled against SDK 23 (Android 6.0). Developers should set targetSdkVersion to 23. The library allows full customization of themes and features.
Integration Steps
1. Add Dependencies
Include the following in your module-level build.gradle file:
dependencies {
compile 'cn.finalteam:galleryfinal:1.4.8.7'
compile 'com.android.support:support-v4:23.1.1'
}
2. Initialize GalleryFinal in Application
Create a configuration with a theme, feature settings, and an image loader. Example:
// Choose or build a theme
ThemeConfig theme = new ThemeConfig.Builder()
.setTitleBarBgColor(Color.parseColor("#FF5722"))
.build();
// Configure features
FunctionConfig functionConfig = new FunctionConfig.Builder()
.setEnableCamera(true)
.setEnableEdit(true)
.setEnableCrop(true)
.setEnableRotate(true)
.setCropSquare(true)
.setEnablePreview(true)
.build();
// Set image loader (e.g., UIL, Glide, Picasso, Fresco)
ImageLoader imageLoader = new UILImageLoader();
CoreConfig coreConfig = new CoreConfig.Builder(context, imageLoader, theme)
.setDebug(BuildConfig.DEBUG)
.setFunctionConfig(functionConfig)
.build();
GalleryFinal.init(coreConfig);
3. Implement an Image Loader
GalleryFinal requires a custom image loader implementing the ImageLoader interface. Existing implementations are avialable for:
- Universal Image Loader
- Glide
- Picasso
- Fresco
- xUtils (versions 2 and 3)
To create a custom loader:
- Implement
ImageLoaderinterface. - In
displayImage(), avoid local and memory caching. - Use provided width and height to set target size.
- Set default placeholder and use
Bitmap.Config.RGB_565to prevent OOM.
4. Launch Gallery Features
Use static methods of GalleryFinal with a request code and result callback:
Single Selection Gallery
GalleryFinal.openGallerySingle(REQUEST_CODE_GALLERY, mOnHanlderResultCallback);
// With custom configuration:
GalleryFinal.openGallerySingle(REQUEST_CODE_GALLERY, functionConfig, mOnHanlderResultCallback);
Multi Selection Gallery
GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, mOnHanlderResultCallback);
FunctionConfig config = new FunctionConfig.Builder(MainActivity.this)
.setMutiSelectMaxSize(8)
.build();
GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, config, mOnHanlderResultCallback);
Camera Capture
GalleryFinal.openCamera(REQUEST_CODE_CAMERA, mOnHanlderResultCallback);
GalleryFinal.openCamera(REQUEST_CODE_CAMERA, functionConfig, mOnHanlderResultCallback);
Crop Image
GalleryFinal.openCrop(REQUEST_CODE_CROP, mOnHanlderResultCallback);
GalleryFinal.openCrop(REQUEST_CODE_CROP, functionConfig, mOnHanlderResultCallback);
Edit Image
GalleryFinal.openEdit(REQUEST_CODE_EDIT, mOnHanlderResultCallback);
GalleryFinal.openEdit(REQUEST_CODE_EDIT, functionConfig, mOnHanlderResultCallback);
Configuration Details
FunctionConfig Builder Methods
| Method | Description |
|---|---|
setMutiSelect(boolean) |
Enable multi-selection |
setMutiSelectMaxSize(int) |
Max number of selectable items |
setEnableEdit(boolean) |
Enable editing (crop/rotate) |
setEnableCrop(boolean) |
Enable cropping |
setEnableRotate(boolean) |
Enable rotation |
setEnableCamera(boolean) |
Enable camera |
setCropWidth(int) |
Crop output width |
setCropHeight(int) |
Crop output height |
setCropSquare(boolean) |
Force square crop |
setSelected(List) |
Pre-select items |
setFilter(List) |
Hide items from gallery |
takePhotoFolter(File) |
Camera save directory |
setRotateReplaceSource(boolean) |
Replace original with rotated version |
setCropReplaceSource(boolean) |
Replace original with cropped version |
setForceCrop(boolean) |
Enter crop immediately (single selection only) |
setForceCropEdit(boolean) |
Show edit icons during forced crop |
setEnablePreview(boolean) |
Enable preview functionality |
Theme Customization
Built-in themes: DEFAULT, DARK, CYAN, ORANGE, GREEN, TEAL. To create a custom theme:
GalleryTheme theme = new GalleryTheme.Builder()
.setTitleBarBgColor(Color.rgb(0xFF, 0x57, 0x22))
.setTitleBarTextColor(Color.BLACK)
.setTitleBarIconColor(Color.BLACK)
.setFabNornalColor(Color.RED)
.setFabPressedColor(Color.BLUE)
.setCheckNornalColor(Color.WHITE)
.setCheckSelectedColor(Color.BLACK)
.setIconBack(R.mipmap.ic_action_previous_item)
.setIconRotate(R.mipmap.ic_action_repeat)
.setIconCrop(R.mipmap.ic_action_crop)
.setIconCamera(R.mipmap.ic_action_camera)
.build();
GalleryFinal.init(theme);
Important ThemeConfig methods:
setTitleBarTextColor,setTitleBarBgColor,setTitleBarIconColorsetCheckNornalColor,setCheckSelectedColorsetCropControlColorsetFabNornalColor,setFabPressedColorsetIconBack,setIconCamera,setIconCrop,setIconRotate,setIconClear,setIconFolderArrow,setIconDelete,setIconCheck,setIconFab,setIconPreviewsetEditPhotoBgTexture,setPreviewBg
CoreConfig Builder
Builder(Context, ImageLoader, ThemeConfig): Mandatory parameters.setDebug(boolean): Enable debug logs.setEditPhotoCacheFolder(File): Cache directory for edit operations.setTakePhotoFolder(File): Camera output directory (default:/sdcard/DICM/GalleryFinal/).setFunctionConfig(FunctionConfig): Global feature configuration.setNoAnimcation(): Disable animations.setPauseOnScrollListener(PauseOnScrollListener): Optimize image loading during scrolling.
Required Permissions
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
ProGuard Rules
-keep class cn.finalteam.galleryfinal.widget.*{*;}
-keep class cn.finalteam.galleryfinal.widget.crop.*{*;}
-keep class cn.finalteam.galleryfinal.widget.zoonview.*{*;}
Acknowledgments
This library uses android-crop for image cropping and PhotoView for zoom support.