Skip to content

Commit 50e549d

Browse files
committed
Multi choose images added
1 parent 44bca60 commit 50e549d

File tree

24 files changed

+1291
-39
lines changed

24 files changed

+1291
-39
lines changed

EasyMediaPicker/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ android {
1515
consumerProguardFiles "consumer-rules.pro"
1616
}
1717

18+
buildFeatures {
19+
viewBinding = true
20+
}
1821
buildTypes {
1922
release {
2023
minifyEnabled false
@@ -50,4 +53,6 @@ dependencies {
5053

5154
//Glide
5255
implementation 'com.github.bumptech.glide:glide:4.13.1'
56+
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
57+
implementation 'io.reactivex.rxjava3:rxjava:3.1.2'
5358
}

EasyMediaPicker/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010

1111
<application>
1212
<activity android:name="com.bn.easypicker.mutils.request_permission.RequestStoragePermissionActivity" />
13+
<activity android:name=".multiChoose.GalleryActivity"
14+
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"/>
1315
</application>
1416
</manifest>

EasyMediaPicker/src/main/java/com/bn/easypicker/EasyPicker.kt

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import androidx.lifecycle.lifecycleScope
1717
import com.bn.easypicker.MediaStoreUtils.deleteUriFile
1818
import com.bn.easypicker.listeners.OnAttachmentTypeSelected
1919
import com.bn.easypicker.listeners.OnCaptureMedia
20+
import com.bn.easypicker.multiChoose.Constants
21+
import com.bn.easypicker.multiChoose.GalleryActivity
2022
import com.bn.easypicker.mutils.FilesVersionUtil
2123
import com.bn.easypicker.mutils.PermissionUtils
2224
import 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
}

EasyMediaPicker/src/main/java/com/bn/easypicker/FragmentEasyPicker.kt

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import androidx.lifecycle.lifecycleScope
1717
import com.bn.easypicker.MediaStoreUtils.deleteUriFile
1818
import com.bn.easypicker.listeners.OnAttachmentTypeSelected
1919
import com.bn.easypicker.listeners.OnCaptureMedia
20+
import com.bn.easypicker.multiChoose.Constants
21+
import com.bn.easypicker.multiChoose.GalleryActivity
2022
import com.bn.easypicker.mutils.FilesVersionUtil
2123
import com.bn.easypicker.mutils.PermissionUtils
2224
import com.bn.easypicker.mutils.UploadImages
@@ -40,6 +42,7 @@ class FragmentEasyPicker(
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 =
@@ -70,6 +73,8 @@ class FragmentEasyPicker(
7073
var sheetBackgroundColor: Int = R.color.white
7174
var btnBackground: Int = R.drawable.bg_et_silver
7275
var textColor: Int = R.color.black
76+
var maximumSelectionLimit: Int = 20
77+
7378
var mListener = object : OnCaptureMedia {
7479
override fun onCaptureMedia(request: Int, files: ArrayList<FileResource>?) {
7580
}
@@ -86,7 +91,12 @@ class FragmentEasyPicker(
8691
this.sheetBackgroundColor = backgroundColor
8792
return this
8893
}
89-
94+
fun setMaxSelectionLimit(
95+
limit: Int
96+
): Builder {
97+
this.maximumSelectionLimit = limit
98+
return this
99+
}
90100
fun setIconsAndTextColor(
91101
cameraIcon: Int? = null,
92102
galleryIcon: Int? = null,
@@ -186,6 +196,45 @@ class FragmentEasyPicker(
186196
}
187197
}
188198

199+
fun getImagesList(data: Intent): ArrayList<Uri> {
200+
if (data != null && data.hasExtra(Constants.BUNDLE_IMAGE_PICKED_SUCCESS) && data.getBooleanExtra(
201+
Constants.BUNDLE_IMAGE_PICKED_SUCCESS,
202+
false
203+
)
204+
) {
205+
return data?.getParcelableArrayListExtra(Constants.BUNDLE_SELECTED_IMAGE_RESULT)
206+
?: arrayListOf()
207+
}
208+
return arrayListOf()
209+
}
210+
private var multiImageLauncherFromCustomGallery =
211+
fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
212+
if (result.resultCode == Activity.RESULT_OK) {
213+
val data = result.data
214+
val uriArray = data?.let { getImagesList(it) }
215+
var images: ArrayList<FileResource> = ArrayList()
216+
217+
if (uriArray != null) {
218+
for (imageUri in uriArray) {
219+
images.add(
220+
FileResource(
221+
uri = imageUri,
222+
path = FilesVersionUtil.getRealPathFromUri(
223+
mContext,
224+
imageUri
225+
)
226+
)
227+
)
228+
}
229+
if (images.isNotEmpty()) mListener.onCaptureMedia(request, files = images)
230+
} else {
231+
Toast.makeText(mContext, "Can't pick your images", Toast.LENGTH_SHORT)
232+
}
233+
234+
235+
}
236+
}
237+
189238

190239

191240
private var takeImageLauncher =
@@ -365,17 +414,23 @@ class FragmentEasyPicker(
365414
}
366415
}
367416

368-
369417
fun chooseMultipleImages() {
370418
if (checkPermission()) {
371-
val intent = Intent(
372-
Intent.ACTION_PICK,
373-
MediaStore.Images.Media.INTERNAL_CONTENT_URI
374-
).apply {
375-
type = "image/*"
419+
if (Build.VERSION.SDK_INT >= 30) {
420+
val intent = Intent(
421+
Intent.ACTION_PICK,
422+
MediaStore.Images.Media.INTERNAL_CONTENT_URI
423+
).apply {
424+
type = "image/*"
425+
}
426+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
427+
multiImageLauncher.launch(intent)
428+
} else {
429+
val intent = Intent(fragment.requireActivity(), GalleryActivity::class.java)
430+
intent.putExtra(Constants.BUNDLE_SHOW_ALBUMS, true)
431+
intent.putExtra(Constants.BUNDLE_MAX_SELECTION_LIMIT, maximumSelectionLimit)
432+
multiImageLauncherFromCustomGallery.launch(intent)
376433
}
377-
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
378-
multiImageLauncher.launch(intent)
379434
} else {
380435
PickActions.openStorageRequest(fragment.requireActivity(), resultLauncher)
381436
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.bn.easypicker.multiChoose
2+
3+
internal class Constants {
4+
companion object{
5+
internal const val BUNDLE_MAX_SELECTION_LIMIT = "maxSelectionLimit"
6+
internal const val BUNDLE_SHOW_ALBUMS = "showAlbums"
7+
8+
internal const val BUNDLE_SELECTED_IMAGE_RESULT = "selectedImageResult"
9+
internal const val BUNDLE_IMAGE_PICKED_SUCCESS = "pickImagesSuccess"
10+
}
11+
}

0 commit comments

Comments
 (0)