Android Kaki

Build beautiful, usable products using required Components for Android.

Permissionless picture picker — Android | by Pankaj Rai 🇮🇳 | Could 2023


Pankaj Rai

Have you ever ever had a requirement the place the consumer can choose a photograph or video from their gallery, to help such sort of performance you will need to ask the consumer’s permission to entry the media recordsdata.

What’s altering now?
Permissionless strategy to pick media recordsdata the place the app will solely have entry to the pictures the consumer is choosing as an alternative of getting access to all media recordsdata (pictures/movies)

Choose VisualMedia
Whenever you need the consumer to pick a single media file then you should use PickVisualMedia which is able to return a Uri which you should use later to show that media on the display.

Check out the Jetpack Compose associated codebase by including that functionality by means of composability

val mediaPicker = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickVisualMedia(),
onResult = { uri ->
imageUri = uri
})

Use Exercise Outcomes API to outline contract as ActivityResultContracts.PickVisualMedia(), after consumer choose media you’re going to get uri in onResult lambda

The subsequent step is to transform the URI to a bitmap, that is utterly optionally available, you’ll be able to even use the URI with the coil library.

//This logic is just for picture not for video
LaunchedEffect(key1 = imageUri) {
imageUri?.let { uri ->
bitmap = if (Construct.VERSION.SDK_INT MediaStore.Pictures
.Media.getBitmap(localContext.contentResolver,uri)
} else {
val supply = ImageDecoder
.createSource(localContext.contentResolver,uri)
ImageDecoder.decodeBitmap(supply)
}
}
}

Lastly, to show the chosen picture, you’ll be able to set the bitmap to Picture composable.

Picture(bitmap = bitmap.asImageBitmap(), contentDescription = "Chosen picture")

Right here bitmap is a nullable mutable state object like under
var bitmap by mnemonic { mutableStateOf(null) }

Now, the ultimate query is methods to provoke the expertise in order that the consumer can choose a picture or video from the gallery.

When the button is clicked, you should use the thing outlined utilizing the Exercise Outcomes API for its “mediaPicker” and name the launch() methodology.

Choose solely pictures

Button(onClick = {
mediaPicker
.launch(PickVisualMediaRequest(
ActivityResultContracts.PickVisualMedia.ImageOnly
)
)}
) {
Textual content(textual content = "Decide picture solely")
}

Choose video solely

Button(onClick = {
mediaPicker
.launch(PickVisualMediaRequest(
ActivityResultContracts.PickVisualMedia.VideoOnly
)
)}
) {
Textual content(textual content = "Decide video solely")
}

Select a picture or video

Button(onClick = {
mediaPicker
.launch(PickVisualMediaRequest(
ActivityResultContracts.PickVisualMedia.ImageAndVideo
)
)}
) {
Textual content(textual content = "Decide picture or video")
}

In the identical method, once you need the consumer to pick a number of pictures or movies, then as an alternative of “ActivityResultContracts.PickVisualMedia()” you should use “PickMultipleVisualMedia()”

val mediaPicker = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickMultipleVisualMedia(),
onResult = { uriList ->
//deal with checklist of Uris
})

With the discharge of ActivityX 1.7.0, the Picture Picker help library will use the back-ported model supplied by Google Play providers on units operating Android KitKat (4.4) and later.

To allow the reverse handed picture picker add the next service to the Manifest file

android:enabled="false"
android:exported="false">






That is it, with these small steps you have added help for choosing media recordsdata from the library with out explicitly asking for permission.

John Wick: Chapter 4 (FREE) FULLMOVIE The Super Mario Bros Movie avatar 2 Where To Watch Creed 3 Free At Home Knock at the Cabin (2023) FullMovie Where To Watch Ant-Man 3 and the Wasp: Quantumania Cocaine Bear 2023 (FullMovie) Scream 6 Full Movie
Updated: May 4, 2023 — 1:13 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

androidkaki.com © 2023 Android kaki