|
| 1 | +package com.cyxbs.components.utils.extensions |
| 2 | + |
| 3 | +import android.net.Uri |
| 4 | +import androidx.activity.result.ActivityResultLauncher |
| 5 | +import androidx.activity.result.PickVisualMediaRequest |
| 6 | +import androidx.activity.result.contract.ActivityResultContracts |
| 7 | +import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageOnly |
| 8 | +import androidx.fragment.app.Fragment |
| 9 | +import androidx.fragment.app.FragmentActivity |
| 10 | + |
| 11 | +/** |
| 12 | + * Android 图片选择器工具类 |
| 13 | + * |
| 14 | + * 使用 Android 官方的 Photo Picker API,无需申请存储权限 |
| 15 | + * - Android 13+ (API 33+): 使用系统级 Photo Picker |
| 16 | + * - Android 13-: 自动降级为传统文件选择器 |
| 17 | + * |
| 18 | + * @author GuoXiangrui |
| 19 | + * @time 2026/5/17 |
| 20 | + */ |
| 21 | + |
| 22 | + |
| 23 | +/* |
| 24 | +* 如果你需要在多平台上使用图片选择器,则可以参考 map 模块中的 UploadPhotoDialog |
| 25 | +* 使用多平台文件选择库 FileKit https://github.com/vinceglb/FileKit |
| 26 | +* |
| 27 | +* |
| 28 | +* */ |
| 29 | + |
| 30 | +// ==================== Activity 扩展函数 ==================== |
| 31 | + |
| 32 | +/** |
| 33 | + * 注册多选图片的 ActivityResultLauncher |
| 34 | + * |
| 35 | + * @param maxImages 最大可选择图片数量,默认 9 张 |
| 36 | + * @return ActivityResultLauncher<List<Uri>> 用于启动图片选择器 |
| 37 | + * |
| 38 | + * 使用示例: |
| 39 | + * ```kotlin |
| 40 | + * private val pickImagesLauncher = registerForPickMultipleImages(5) { uris -> |
| 41 | + * // 处理选中的图片 URI 列表 |
| 42 | + * if (uris.isNotEmpty()) { |
| 43 | + * handleSelectedImages(uris) |
| 44 | + * } |
| 45 | + * } |
| 46 | + * |
| 47 | + * // 启动选择器 |
| 48 | + * pickImagesLauncher.launchPickImages() |
| 49 | + * ``` |
| 50 | + */ |
| 51 | +fun FragmentActivity.registerForPickMultipleImages( |
| 52 | + maxImages: Int = 9, |
| 53 | + onResult: (List<Uri>) -> Unit |
| 54 | +): ActivityResultLauncher<PickVisualMediaRequest> { |
| 55 | + return registerForActivityResult( |
| 56 | + ActivityResultContracts.PickMultipleVisualMedia(maxImages) |
| 57 | + ) { uris -> |
| 58 | + onResult(uris) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * 注册单选图片的 ActivityResultLauncher |
| 64 | + * |
| 65 | + * @return ActivityResultLauncher<Uri?> 用于启动图片选择器 |
| 66 | + * |
| 67 | + * 使用示例: |
| 68 | + * ```kotlin |
| 69 | + * private val pickImageLauncher = registerForPickSingleImage { uri -> |
| 70 | + * // 处理选中的图片 URI |
| 71 | + * uri?.let { |
| 72 | + * handleSelectedImage(it) |
| 73 | + * } |
| 74 | + * } |
| 75 | + * |
| 76 | + * // 启动选择器 |
| 77 | + * pickImageLauncher.launchPickImages() |
| 78 | + * ``` |
| 79 | + */ |
| 80 | +fun FragmentActivity.registerForPickSingleImage( |
| 81 | + onResult: (Uri?) -> Unit |
| 82 | +): ActivityResultLauncher<PickVisualMediaRequest> { |
| 83 | + return registerForActivityResult( |
| 84 | + ActivityResultContracts.PickVisualMedia() |
| 85 | + ) { uri -> |
| 86 | + onResult(uri) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// ==================== Fragment 扩展函数 ==================== |
| 91 | + |
| 92 | +/** |
| 93 | + * Fragment 中注册多选图片的 ActivityResultLauncher |
| 94 | + * |
| 95 | + * @param maxImages 最大可选择图片数量,默认 9 张 |
| 96 | + * @return ActivityResultLauncher<List<Uri>> 用于启动图片选择器 |
| 97 | + */ |
| 98 | +fun Fragment.registerForPickMultipleImages( |
| 99 | + maxImages: Int = 9, |
| 100 | + onResult: (List<Uri>) -> Unit |
| 101 | +): ActivityResultLauncher<PickVisualMediaRequest> { |
| 102 | + return registerForActivityResult( |
| 103 | + ActivityResultContracts.PickMultipleVisualMedia(maxImages) |
| 104 | + ) { uris -> |
| 105 | + onResult(uris) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Fragment 中注册单选图片的 ActivityResultLauncher |
| 111 | + * |
| 112 | + * @return ActivityResultLauncher<Uri?> 用于启动图片选择器 |
| 113 | + */ |
| 114 | +fun Fragment.registerForPickSingleImage( |
| 115 | + onResult: (Uri?) -> Unit |
| 116 | +): ActivityResultLauncher<PickVisualMediaRequest> { |
| 117 | + return registerForActivityResult( |
| 118 | + ActivityResultContracts.PickVisualMedia() |
| 119 | + ) { uri -> |
| 120 | + onResult(uri) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ==================== 便捷启动函数 ==================== |
| 125 | + |
| 126 | +/** |
| 127 | + * 启动多选图片选择器(仅图片) |
| 128 | + * |
| 129 | + * 使用示例: |
| 130 | + * ```kotlin |
| 131 | + * pickImagesLauncher.launchPickImages() |
| 132 | + * ``` |
| 133 | + */ |
| 134 | +fun ActivityResultLauncher<PickVisualMediaRequest>.launchPickImages() { |
| 135 | + this.launch(PickVisualMediaRequest(ImageOnly)) |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * 启动多选图片选择器(图片和视频) |
| 140 | + * |
| 141 | + * 使用示例: |
| 142 | + * ```kotlin |
| 143 | + * pickMediaLauncher.launchPickImagesAndVideos() |
| 144 | + * ``` |
| 145 | + */ |
| 146 | +fun ActivityResultLauncher<PickVisualMediaRequest>.launchPickImagesAndVideos() { |
| 147 | + this.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)) |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * 启动多选图片选择器(仅视频) |
| 152 | + * |
| 153 | + * 使用示例: |
| 154 | + * ```kotlin |
| 155 | + * pickVideosLauncher.launchPickVideos() |
| 156 | + * ``` |
| 157 | + */ |
| 158 | +fun ActivityResultLauncher<PickVisualMediaRequest>.launchPickVideos() { |
| 159 | + this.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly)) |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * 启动多选图片选择器(指定 MIME 类型) |
| 164 | + * |
| 165 | + * @param mimeType MIME 类型,例如 "image/gif" |
| 166 | + * |
| 167 | + * 使用示例: |
| 168 | + * ```kotlin |
| 169 | + * pickGifLauncher.launchPickByMimeType("image/gif") |
| 170 | + * ``` |
| 171 | + */ |
| 172 | +fun ActivityResultLauncher<PickVisualMediaRequest>.launchPickByMimeType(mimeType: String) { |
| 173 | + this.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.SingleMimeType(mimeType))) |
| 174 | +} |
0 commit comments