@@ -17,6 +17,8 @@ import androidx.lifecycle.lifecycleScope
1717import com.bn.easypicker.MediaStoreUtils.deleteUriFile
1818import com.bn.easypicker.listeners.OnAttachmentTypeSelected
1919import com.bn.easypicker.listeners.OnCaptureMedia
20+ import com.bn.easypicker.multiChoose.Constants
21+ import com.bn.easypicker.multiChoose.GalleryActivity
2022import com.bn.easypicker.mutils.FilesVersionUtil
2123import com.bn.easypicker.mutils.PermissionUtils
2224import com.bn.easypicker.mutils.UploadImages
@@ -40,6 +42,7 @@ class EasyPicker(
4042 private val textColor: Int = builder.textColor
4143 private val backgroundColor: Int = builder.sheetBackgroundColor
4244 private val btnBackground: Int = builder.btnBackground
45+ private val maximumSelectionLimit: Int = builder.maximumSelectionLimit
4346
4447
4548 private val resultLauncher =
@@ -69,6 +72,7 @@ class EasyPicker(
6972 var galleryIcon: Int = R .drawable.ic_galery
7073 var sheetBackgroundColor: Int = R .color.white
7174 var btnBackground: Int = R .drawable.bg_et_silver
75+ var maximumSelectionLimit: Int = 20
7276 var textColor: Int = R .color.black
7377 var mListener = object : OnCaptureMedia {
7478 override fun onCaptureMedia (
@@ -90,7 +94,12 @@ class EasyPicker(
9094 this .sheetBackgroundColor = backgroundColor
9195 return this
9296 }
93-
97+ fun setMaxSelectionLimit (
98+ limit : Int
99+ ): Builder {
100+ this .maximumSelectionLimit = limit
101+ return this
102+ }
94103 fun setIconsAndTextColor (
95104 cameraIcon : Int? = null,
96105 galleryIcon : Int? = null,
@@ -136,10 +145,15 @@ class EasyPicker(
136145 } catch (e: Exception ) {
137146 try {
138147 Log .e(" ExceptionVideo" , " >>> Exception Video First: ${e.message} " )
139- resulting.add(FileResource (
140- uri = imageUri,
141- path = FilesVersionUtil .getRealPathFromUri(mContext, imageUri)
142- ))
148+ resulting.add(
149+ FileResource (
150+ uri = imageUri,
151+ path = FilesVersionUtil .getRealPathFromUri(
152+ mContext,
153+ imageUri
154+ )
155+ )
156+ )
143157 } catch (e: Exception ) {
144158 Log .e(" ExceptionVideo" , " >>> Exception Video: ${e.message} " )
145159 }
@@ -153,8 +167,8 @@ class EasyPicker(
153167 act.registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) { result ->
154168 if (result.resultCode == Activity .RESULT_OK ) {
155169 if (result.data?.clipData != null ) {
170+ Log .v(" Filessss" , " ResultOk" )
156171 val count: Int = result.data?.clipData?.itemCount ? : 0
157-
158172 var images: ArrayList <FileResource > = ArrayList ()
159173 for (i in 0 until count) {
160174 val imageUri: Uri = try {
@@ -165,7 +179,46 @@ class EasyPicker(
165179 result.data?.clipData?.getItemAt(i)?.uri!! as Bitmap
166180 )!!
167181 }
182+ images.add(
183+ FileResource (
184+ uri = imageUri,
185+ path = FilesVersionUtil .getRealPathFromUri(
186+ mContext,
187+ imageUri
188+ )
189+ )
190+ )
191+ }
192+ Log .v(" Filessss" , " ${images.size} " )
193+ if (images.isNotEmpty()) mListener.onCaptureMedia(request, files = images)
194+ } else {
195+ Toast .makeText(mContext, " Can't pick your images" , Toast .LENGTH_SHORT )
196+ }
168197
198+ }
199+ }
200+
201+ fun getImagesList (data : Intent ): ArrayList <Uri > {
202+ if (data != null && data.hasExtra(Constants .BUNDLE_IMAGE_PICKED_SUCCESS ) && data.getBooleanExtra(
203+ Constants .BUNDLE_IMAGE_PICKED_SUCCESS ,
204+ false
205+ )
206+ ) {
207+ return data?.getParcelableArrayListExtra(Constants .BUNDLE_SELECTED_IMAGE_RESULT )
208+ ? : arrayListOf ()
209+ }
210+ return arrayListOf ()
211+ }
212+
213+ private var multiImageLauncherFromCustomGallery =
214+ act.registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) { result ->
215+ if (result.resultCode == Activity .RESULT_OK ) {
216+ val data = result.data
217+ val uriArray = data?.let { getImagesList(it) }
218+ var images: ArrayList <FileResource > = ArrayList ()
219+
220+ if (uriArray != null ) {
221+ for (imageUri in uriArray) {
169222 images.add(
170223 FileResource (
171224 uri = imageUri,
@@ -176,11 +229,12 @@ class EasyPicker(
176229 )
177230 )
178231 }
179- if (images.isNotEmpty()) mListener.onCaptureMedia(request, files = images)
232+ if (images.isNotEmpty()) mListener.onCaptureMedia(request, files = images)
180233 } else {
181234 Toast .makeText(mContext, " Can't pick your images" , Toast .LENGTH_SHORT )
182235 }
183236
237+
184238 }
185239 }
186240
@@ -204,10 +258,12 @@ class EasyPicker(
204258 } catch (e: Exception ) {
205259 try {
206260 Log .e(" ExceptionVideo" , " >>> Exception Video First: ${e.message} " )
207- resulting.add(FileResource (
208- uri = mPath,
209- path = FilesVersionUtil .getRealPathFromUri(mContext, mPath)
210- ))
261+ resulting.add(
262+ FileResource (
263+ uri = mPath,
264+ path = FilesVersionUtil .getRealPathFromUri(mContext, mPath)
265+ )
266+ )
211267 } catch (e: Exception ) {
212268 Log .e(" ExceptionVideo" , " >>> Exception Video: ${e.message} " )
213269 }
@@ -232,14 +288,24 @@ class EasyPicker(
232288 } catch (e: Exception ) {
233289 try {
234290 Log .e(" ExceptionVideo" , " >>> Exception Video First: ${e.message} " )
235- resulting.add(FileResource (
236- uri = imageUri,
237- path = FilesVersionUtil .getRealPathFromUri(mContext, imageUri)
238- ? : getPathFromURI(mContext, imageUri)
239- ))
291+ resulting.add(
292+ FileResource (
293+ uri = imageUri,
294+ path = FilesVersionUtil .getRealPathFromUri(
295+ mContext,
296+ imageUri
297+ )
298+ ? : getPathFromURI(mContext, imageUri)
299+ )
300+ )
240301 } catch (e: Exception ) {
241302 try {
242- resulting.add(MediaStoreUtils .getResourceByUri(mContext, imageUri))
303+ resulting.add(
304+ MediaStoreUtils .getResourceByUri(
305+ mContext,
306+ imageUri
307+ )
308+ )
243309 } catch (e: Exception ) {
244310 Log .e(" ExceptionVideo" , " >>> Exception Video: ${e.message} " )
245311 }
@@ -316,10 +382,15 @@ class EasyPicker(
316382 } catch (e: Exception ) {
317383 try {
318384 Log .e(" ExceptionVideo" , " >>> Exception Video First: ${e.message} " )
319- resulting.add(FileResource (
320- uri = imageUri,
321- path = FilesVersionUtil .getRealPathFromUri(mContext, imageUri)
322- ))
385+ resulting.add(
386+ FileResource (
387+ uri = imageUri,
388+ path = FilesVersionUtil .getRealPathFromUri(
389+ mContext,
390+ imageUri
391+ )
392+ )
393+ )
323394 } catch (e: Exception ) {
324395 Log .e(" ExceptionVideo" , " >>> Exception Video: ${e.message} " )
325396 }
@@ -349,14 +420,24 @@ class EasyPicker(
349420
350421 fun chooseMultipleImages () {
351422 if (checkPermission()) {
352- val intent = Intent (
353- Intent .ACTION_PICK ,
354- MediaStore .Images .Media .INTERNAL_CONTENT_URI
355- ).apply {
356- type = " image/*"
423+ act.lifecycleScope.launchWhenStarted {
424+ multiImageLauncher
425+ }
426+ if (Build .VERSION .SDK_INT >= 30 ) {
427+ val intent = Intent (
428+ Intent .ACTION_PICK ,
429+ MediaStore .Images .Media .INTERNAL_CONTENT_URI
430+ ).apply {
431+ type = " image/*"
432+ }
433+ intent.putExtra(Intent .EXTRA_ALLOW_MULTIPLE , true )
434+ multiImageLauncher.launch(intent)
435+ } else {
436+ val intent = Intent (act, GalleryActivity ::class .java)
437+ intent.putExtra(Constants .BUNDLE_SHOW_ALBUMS , true )
438+ intent.putExtra(Constants .BUNDLE_MAX_SELECTION_LIMIT , maximumSelectionLimit)
439+ multiImageLauncherFromCustomGallery.launch(intent)
357440 }
358- intent.putExtra(Intent .EXTRA_ALLOW_MULTIPLE , true )
359- multiImageLauncher.launch(intent)
360441 } else {
361442 PickActions .openStorageRequest(act, resultLauncher)
362443 }
0 commit comments