티스토리 뷰

Android-EN

How can i use Android photo picker

녹수녹두 2022. 12. 16. 01:10
반응형

 

Hi i am android developer who works in Korea and i was born in Korea and raised.

I am studying english and this is part of it. I know there are a lot of mistakes but if you let me know those i'll really appreciate it.

 

photo picker(android developer link)

 

Before Android 13, you need to request 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

to access photos or videos.

 

But from Android 13, you can access these media files with [photo picker] without requesting permission.

If you use it, user can choose the files app can access. if you have a experience of iPhone you are familiar with


1. You can use [photo picker] if the device meets the following criteria

 

The first important thing is that from Android 11, you can use [photo picker] and

you need to use androidx.activity:activity  more than high or equal 1.6.1

 

You can choose either below 

  • PickVisualMedia    -> select only on media file
  • PickMultipleVisualMedia  -> select multiple meida files

There are examples

// need only one media file
val pickMedia = registerForActivityResult(PickVisualMedia()) { uri ->
    if (uri != null) {
        // success
    } else {
        // no choise
    }
}
// image and video
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageAndVideo))

// only image
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))

// only video
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.VideoOnly))

// you can set the avilable type
val mimeType = "image/gif"
pickMedia.launch(PickVisualMediaRequest(PickVisualMedia.SingleMimeType(mimeType)))
// If you need to multiple media files
val pickMultipleMedia =
        registerForActivityResult(PickMultipleVisualMedia(5)) { uris ->
.
    if (uris.isNotEmpty()) {
       // Result is list type
    } else {
       // 
    }
}

 

And you can see the result below.

Left is less than Android 13, right is Android 13

 

If you need to check app can use photo picker, you can use this code. 

This code need CompileSdkVersion is 33 or high.

import android.os.ext.SdkExtensions.getExtensionVersion

private fun isPhotoPickerAvailable(): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        true
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        getExtensionVersion(Build.VERSION_CODES.R) >= 2
    } else {
        false
    }
}

fun handlePhotoPickerLaunch() {
    if (isPhotoPickerAvailable()) {
      
    } else {
     
    }
}
반응형