How to pick GIF files only

Viewed 310

I want to pick Images(JPG, PNG) and GIF(gif) files separately. This works for most Gallery apps and file managers except Google Photos app. When I pick images or gifs using Google Photos app, all images are displayed i.e. JPG, PNG, GIF, BMP, etc. How can I tell Google Photos to allow picking only selected type of files?

Here's my code:

val contentPicker = activity.registerForActivityResult(
    ActivityResultContracts.GetContent()
) { uri: Uri? ->
    // Use Picked URI
}

val mime = when {
    MediaType.isPhoto(mediaType) -> "image/*" // or image/jpg or image/png
    MediaType.isGif(mediaType) -> "image/gif"
    else -> throw RuntimeException("Wrong Media Type to pick: $mediaType")
}

contentPicker.launch(mime)

If there's another way besides this(maybe targeting Google Photos only), please share as an answer. I couldn't find any relevant question with proper answer. I want to use system apps only to pick content.

picker activity

Edit (Nov 23, 2021)

Even if I use following intent to open Files app, a user can just go to Side Bar and select Photos app. Files app prevents picking gif/heic images. But, when Photos app is opened from within the Files app, user can select gif/heic images without any restriction.

fun getPickPhotoIntent(): Intent {

    val pickIntent = Intent(Intent.ACTION_GET_CONTENT)
    pickIntent.addCategory(Intent.CATEGORY_OPENABLE)
    pickIntent.type = "image/jpg"

    val mimeTypes = arrayOf("image/bmp", "image/jpeg", "image/png")
    pickIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
    pickIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)

    return pickIntent
}
1 Answers
fun getPickPhotoIntent(): Intent {

val pickIntent = Intent(com.google.android.apps.photos.picker.external.ExternalPickerActivity)//package name Used
pickIntent.addCategory(Intent.CATEGORY_OPENABLE)
pickIntent.type = "image/jpg"

val mimeTypes = arrayOf("image/bmp", "image/jpeg", "image/png")
pickIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
pickIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)

return pickIntent
}
Related