Android Photo Picker

2 min read

第一种方式
val pictureLauncher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent(),
onResult = { uri ->
Log.d(MainActivity.TAG, "GetContent= $uri")
}
)
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
pictureLauncher.launch("image/*")
}
) {
Text(text = "GetContent, launch Image/* ")
}
这种方式打开之后,Gallery,Google Photos,System Traces, Clash meta for Android都出现了
第二种方式
val pictureLauncher2 =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult(),
onResult = { uri ->
Log.d(MainActivity.TAG, "pictureLauncher2: selected image uri = $uri")
})
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
val pickIntent =
Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI
)
pictureLauncher2.launch(pickIntent)
}
) {
Text(text = "ACTION_PICK, INTERNAL_CONTENT_URI")
}
需要先从 Google photos 和 Gallery App 挑选一个
第三种方式
val pictureLauncher3 =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult(),
onResult = { uri ->
Log.d(MainActivity.TAG, "pictureLauncher2: selected image uri = $uri")
})
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
val pickIntent =
Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
pictureLauncher3.launch(pickIntent)
}
) {
Text(text = "ACTION_PICK, EXTERNAL_CONTENT_URI")
}
需要先从 Google photos 和 Gallery App 挑选一个
第四种方式
val pictureLauncher4 =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult(),
onResult = { uri ->
Log.d(MainActivity.TAG, "pictureLauncher2: selected image uri = $uri")
})
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
val pickIntent =
Intent(
MediaStore.ACTION_PICK_IMAGES,
).apply {
type = "image/*"
}
pictureLauncher4.launch(pickIntent)
}
) {
Text(text = "ACTION_PICK_IMAGES")
}
BottomSheetFragment 直接出现了 album 和 photos 两个 tab
第五种方式
val pictureLauncher5 =
rememberLauncherForActivityResult(contract = PickContentLegacyDocumentTree()
.apply {
},
onResult = { uri ->
Log.d(MainActivity.TAG, "pictureLauncher2: selected image uri = $uri")
})
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
pictureLauncher5.launch(Unit)
}
) {
Text(text = "ACTION_OPEN_DOCUMENT,image/* ")
}
class PickContentLegacyDocumentTree : ActivityResultContract<Unit, Uri?>() {
override fun createIntent(context: Context, input: Unit): Intent {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*"))
return intent
}
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
if (resultCode == ComponentActivity.RESULT_OK) {
val data = intent?.data
return data
}
return null
}
}
出现了 Clash Meta For Android 和 System Traces 图片,进入到系统的页面
第六种方式
val pictureLauncher6 = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
if (uri != null) {
Log.d("PhotoPicker", "Selected URI: $uri")
} else {
Log.d("PhotoPicker", "No media selected")
}
}
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
pictureLauncher6.launch(
PickVisualMediaRequest(
ActivityResultContracts.PickVisualMedia.ImageOnly
)
)
}
) {
Text(text = "PickVisualMediaRequest, ImageOnly ")
}
出现了 Clash Meta For Android 和 System Traces 图片,进入到系统的页面
第七种方式
val multiplePhotoPicker = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickMultipleVisualMedia(maxItems = 2)
) {
Log.d(MainActivity.TAG, "MainScreen:multiplePhotoPicker uris : $it")
}
Button(
modifier = Modifier.wrapContentSize(),
onClick = {
multiplePhotoPicker.launch(
PickVisualMediaRequest(
ActivityResultContracts.PickVisualMedia.ImageOnly
)
)
}
) {
Text(text = "PickMultipleVisualMedia, maxItems = 2 ")
}
出现了 Clash Meta For Android 和 System Traces 图片,进入到系统的页面
具体可以看这个代码库。Github
1
Subscribe to my newsletter
Read articles from Matthew directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by