A customizable, modern media/document picker for Android with support for image and video capture, file selection, and Jetpack's ActivityResult API integration.
- π Document Picker
- π· Image Capture
- π₯ Video Capture
- πΌοΈ Pick Images & Videos from Gallery
- βοΈ Fully customizable popups
- π§© Built-in ActivityResultContracts for Kotlin & Java
repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.chochanaresh:filepicker:<latest-version>'
}
repositories {
mavenCentral()
}
dependencies {
implementation("io.github.chochanaresh:filepicker:<latest-version>")
}
Use the built-in contracts for simplified integration using Jetpack's registerForActivityResult
.
private val launcher = registerForActivityResult(FilePickerResultContracts.ImageCapture()) { result ->
if (result.errorMessage != null) {
Log.e("Picker", result.errorMessage ?: "")
} else {
val uri = result.selectedFileUri
val filePath = result.selectedFilePath
}
}
Contract | Description |
---|---|
ImageCapture() |
Launch camera and capture image |
VideoCapture() |
Record video using camera |
PickMedia() |
Select image/video from gallery |
PickDocumentFile() |
Choose document(s) |
AllFilePicker() |
Show a popup to choose between multiple |
AnyFilePicker() |
Automatically selects contract based on config |
val launcher = registerForActivityResult(FilePickerResultContracts.ImageCapture()) { result -> }
launcher.launch(ImageCaptureConfig())
val launcher = registerForActivityResult(FilePickerResultContracts.VideoCapture()) { result -> }
launcher.launch(VideoCaptureConfig())
val launcher = registerForActivityResult(FilePickerResultContracts.PickMedia()) { result -> }
launcher.launch(PickMediaConfig(allowMultiple = true))
val launcher = registerForActivityResult(FilePickerResultContracts.PickDocumentFile()) { result -> }
launcher.launch(DocumentFilePickerConfig(allowMultiple = true))
val launcher = registerForActivityResult(FilePickerResultContracts.AllFilePicker()) { result -> }
launcher.launch(PickerData())
val launcher = registerForActivityResult(FilePickerResultContracts.AnyFilePicker()) { result -> }
launcher.launch(ImageCaptureConfig()) // Or any config subclass
data class FilePickerResult(
val selectedFileUri: Uri? = null,
val selectedFileUris: List<Uri>? = null,
val selectedFilePath: String? = null,
val selectedFilePaths: List<String>? = null,
val errorMessage: String? = null
)
DocumentFilePickerConfig(
popUpIcon = R.drawable.ic_file,
popUpText = "File Media",
allowMultiple = true,
maxFiles = 10,
mMimeTypes = listOf("application/pdf", "image/*")
)
ImageCaptureConfig(
popUpIcon = R.drawable.ic_camera,
popUpText = "Camera",
mFolder = File(cacheDir, "Images"),
fileName = "image_${System.currentTimeMillis()}.jpg",
isUseRearCamera = true
)
VideoCaptureConfig(
popUpIcon = R.drawable.ic_video,
popUpText = "Video",
mFolder = File(cacheDir, "Videos"),
fileName = "video_${System.currentTimeMillis()}.mp4",
maxSeconds = 60,
maxSizeLimit = 20L * 1024 * 1024,
isHighQuality = true
)
PickMediaConfig(
popUpIcon = R.drawable.ic_media,
popUpText = "Pick Media",
allowMultiple = true,
maxFiles = 5,
mPickMediaType = PickMediaType.ImageAndVideo
)
PickMediaType Options
PickMediaType.ImageOnly
PickMediaType.VideoOnly
PickMediaType.ImageAndVideo
val pickerData = PickerData(
mPopUpConfig = PopUpConfig(
chooserTitle = "Choose Option",
mPopUpType = PopUpType.BOTTOM_SHEET,
mOrientation = Orientation.VERTICAL,
cornerSize = 12f
),
listIntents = listOf(
ImageCaptureConfig(),
VideoCaptureConfig(),
PickMediaConfig(),
DocumentFilePickerConfig()
)
)
This guide outlines the changes from the old code to the new FilePicker
implementation, focusing on the transition from the Builder pattern to ActivityResultContract
.
-
Package and Class Changes:
The package has changed fromcom.nareshchocha.filepickerlibrary.ui
tocom.nareshchocha.filepickerlibrary
. -
Removal of
Builder
Class:
TheBuilder
class is no longer needed. The new code utilizesActivityResultContract
for handling file picker actions. -
Introduction of
ActivityResultContracts
:
File picker operations are now handled by specificActivityResultContract
classes, such asImageCapture
,VideoCapture
, andPickMedia
. -
Logging Support:
A newisLoggingEnabled
flag allows enabling logging in the contracts for debugging.
The Builder
class is no longer needed. You should transition to using ActivityResultContracts
instead.
You can now handle file picker actions with specific contracts. For example:
- Old Code:
fun imageCaptureBuild(mImageCaptureConfig: ImageCaptureConfig?): Intent = ImageCaptureActivity.getInstance(context, mImageCaptureConfig)
- New Code:
val imageCaptureResult = registerForActivityResult(FilePickerResultContracts.ImageCapture()) { result -> // Handle result }
- Android 5.0 (API 21) and above
- Supports Kotlin & Java
Contributions are always welcome!
Please fork and submit PRs with clear commit history.
- π§ [email protected]
- π¬ Join our Slack
- Buy me a Coffee
Copyright 2023 Naresh chocha
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.