Skip to content

Commit f6a7fa2

Browse files
authored
Merge pull request #64 from Marchuck/feature/mime_types
Limit mime types while choosing a gallery image
2 parents 23188cc + 8f01d2b commit f6a7fa2

File tree

7 files changed

+70
-38
lines changed

7 files changed

+70
-38
lines changed

imagepicker/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ android {
2020
}
2121

2222
buildTypes {
23+
debug{
24+
25+
}
2326
release {
2427
minifyEnabled false
2528
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ open class ImagePicker {
3939

4040
internal const val EXTRA_ERROR = "extra.error"
4141
internal const val EXTRA_FILE_PATH = "extra.file_path"
42+
internal const val EXTRA_GALLERY_MIME_TYPES = "extra.gallery.mime.types"
4243

4344
/**
4445
* Use this to use ImagePicker in Activity Class
@@ -96,6 +97,9 @@ open class ImagePicker {
9697
// Image Provider
9798
private var imageProvider = BOTH
9899

100+
//mime types restrictions for gallery. by default all mime types are valid
101+
private var mimeTypes: Array<String> = emptyArray()
102+
99103
/*
100104
* Crop Parameters
101105
*/
@@ -149,6 +153,17 @@ open class ImagePicker {
149153
return this
150154
}
151155

156+
/**
157+
* restrict mime types during gallery fetching, for instance if you do not want GIF images,
158+
* you can use arrayOf("image/png","image/jpeg","image/jpg")
159+
* by default array is empty, which indicates no additional restrictions, just images
160+
* @param mimeTypes
161+
*/
162+
fun galleryMimeTypes(mimeTypes: Array<String>): Builder {
163+
this.mimeTypes = mimeTypes
164+
return this
165+
}
166+
152167
/**
153168
* Set an aspect ratio for crop bounds.
154169
* User won't see the menu with other ratios options.
@@ -270,19 +285,21 @@ open class ImagePicker {
270285
* Get Bundle for ImagePickerActivity
271286
*/
272287
private fun getBundle(): Bundle {
273-
val bundle = Bundle()
274-
bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)
288+
return Bundle().apply {
289+
putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)
275290

276-
bundle.putBoolean(EXTRA_CROP, crop)
277-
bundle.putFloat(EXTRA_CROP_X, cropX)
278-
bundle.putFloat(EXTRA_CROP_Y, cropY)
291+
putStringArray(EXTRA_GALLERY_MIME_TYPES, mimeTypes)
279292

280-
bundle.putInt(EXTRA_MAX_WIDTH, maxWidth)
281-
bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight)
293+
putBoolean(EXTRA_CROP, crop)
282294

283-
bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)
295+
putFloat(EXTRA_CROP_X, cropX)
296+
putFloat(EXTRA_CROP_Y, cropY)
284297

285-
return bundle
298+
putInt(EXTRA_MAX_WIDTH, maxWidth)
299+
putInt(EXTRA_MAX_HEIGHT, maxHeight)
300+
301+
putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)
302+
}
286303
}
287304

288305
/**

imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ class ImagePickerActivity : AppCompatActivity() {
8989
// Retrieve Image Provider
9090
val provider: ImageProvider? =
9191
intent?.getSerializableExtra(ImagePicker.EXTRA_IMAGE_PROVIDER) as ImageProvider?
92+
val mimeTypes: Array<String> = intent?.getStringArrayExtra(ImagePicker.EXTRA_GALLERY_MIME_TYPES) ?: emptyArray()
9293

9394
// Create Gallery/Camera Provider
9495
when (provider) {
9596
ImageProvider.GALLERY -> {
96-
mGalleryProvider = GalleryProvider(this)
97+
mGalleryProvider = GalleryProvider(this, mimeTypes)
9798
// Pick Gallery Image
9899
savedInstanceState ?: mGalleryProvider?.startIntent()
99100
}

imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import java.io.File
1818
* @version 1.0
1919
* @since 04 January 2019
2020
*/
21-
class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
21+
class GalleryProvider(activity: ImagePickerActivity, private val mimeTypes: Array<String>) :
22+
BaseProvider(activity) {
2223

2324
companion object {
2425
/**
@@ -48,15 +49,15 @@ class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
4849
if (!PermissionUtil.isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
4950
requestPermissions(activity, REQUIRED_PERMISSIONS, PERMISSION_INTENT_REQ_CODE)
5051
} else {
51-
startGalleryIntent()
52+
startGalleryIntent(mimeTypes)
5253
}
5354
}
5455

5556
/**
5657
* Start Gallery Intent
5758
*/
58-
private fun startGalleryIntent() {
59-
val galleryIntent = IntentUtils.getGalleryIntent(activity)
59+
private fun startGalleryIntent(mimeTypes: Array<String>) {
60+
val galleryIntent = IntentUtils.getGalleryIntent(activity, mimeTypes)
6061
activity.startActivityForResult(galleryIntent, GALLERY_INTENT_REQ_CODE)
6162
}
6263

@@ -68,7 +69,7 @@ class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
6869
// Check again if permission is granted
6970
if (PermissionUtil.isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
7071
// Permission is granted, Start Camera Intent
71-
startGalleryIntent()
72+
startGalleryIntent(mimeTypes)
7273
} else {
7374
// Exit with error message
7475
setError(getString(R.string.permission_gallery_denied))

imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/IntentUtils.kt

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,42 @@ object IntentUtils {
2222
/**
2323
* @return Intent Gallery Intent
2424
*/
25-
fun getGalleryIntent(context: Context): Intent {
26-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
27-
var intent = getGalleryDocumentIntent()
28-
if (intent.resolveActivity(context.packageManager) == null) {
29-
// No Activity found that can handle this intent.
30-
intent = getGalleryPickIntent()
25+
fun getGalleryIntent(context: Context, mimeTypes: Array<String>): Intent {
26+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
27+
val intent = getGalleryDocumentIntent(mimeTypes)
28+
if (intent.resolveActivity(context.packageManager) != null) {
29+
return intent
3130
}
32-
intent
33-
} else {
34-
getGalleryPickIntent()
3531
}
32+
return getLegacyGalleryPickIntent(mimeTypes)
3633
}
3734

3835
/**
3936
* @return Intent Gallery Document Intent
4037
*/
41-
private fun getGalleryDocumentIntent(): Intent {
38+
private fun getGalleryDocumentIntent(mimeTypes: Array<String>): Intent {
4239
// Show Document Intent
43-
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
44-
40+
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).applyImageTypes(mimeTypes)
4541
intent.addCategory(Intent.CATEGORY_OPENABLE)
46-
47-
// Apply filter to show image only in intent
48-
intent.type = "image/*"
49-
5042
return intent
5143
}
5244

5345
/**
5446
* @return Intent Gallery Pick Intent
5547
*/
56-
private fun getGalleryPickIntent(): Intent {
48+
private fun getLegacyGalleryPickIntent(mimeTypes: Array<String>): Intent {
5749
// Show Gallery Intent, Will open google photos
58-
val intent = Intent(Intent.ACTION_PICK)
50+
val intent = Intent(Intent.ACTION_PICK).applyImageTypes(mimeTypes)
51+
return intent
52+
}
5953

54+
private fun Intent.applyImageTypes(mimeTypes: Array<String>): Intent {
6055
// Apply filter to show image only in intent
61-
intent.type = "image/*"
62-
63-
return intent
56+
type = "image/*"
57+
if (mimeTypes.isNotEmpty()) {
58+
putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
59+
}
60+
return this
6461
}
6562

6663
/**
@@ -71,7 +68,8 @@ object IntentUtils {
7168

7269
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
7370
// authority = com.github.dhaval2404.imagepicker.provider
74-
val authority = context.packageName + context.getString(R.string.image_picker_provider_authority_suffix)
71+
val authority =
72+
context.packageName + context.getString(R.string.image_picker_provider_authority_suffix)
7573
val photoURI = FileProvider.getUriForFile(context, authority, file)
7674
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
7775
} else {

imagepicker/src/main/res/xml/image_picker_provider_paths.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
<external-path
44
name="external_files"
55
path="." />
6+
7+
<external-cache-path
8+
name="external_files"
9+
path="." />
610
</paths>

sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import androidx.appcompat.app.AppCompatActivity
1313
import com.github.dhaval2404.imagepicker.ImagePicker
1414
import com.github.dhaval2404.imagepicker.sample.util.FileUtil
1515
import com.github.dhaval2404.imagepicker.sample.util.IntentUtil
16-
import java.io.File
1716
import kotlinx.android.synthetic.main.activity_main.*
1817
import kotlinx.android.synthetic.main.content_camera_only.*
1918
import kotlinx.android.synthetic.main.content_gallery_only.*
2019
import kotlinx.android.synthetic.main.content_profile.*
20+
import java.io.File
2121

2222
class MainActivity : AppCompatActivity() {
2323

@@ -71,6 +71,14 @@ class MainActivity : AppCompatActivity() {
7171
.crop()
7272
// User can only select image from Gallery
7373
.galleryOnly()
74+
75+
.galleryMimeTypes( //no gif images at all
76+
mimeTypes = arrayOf(
77+
"image/png",
78+
"image/jpg",
79+
"image/jpeg"
80+
)
81+
)
7482
// Image resolution will be less than 1080 x 1920
7583
.maxResultSize(1080, 1920)
7684
.start(GALLERY_IMAGE_REQ_CODE)

0 commit comments

Comments
 (0)