-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImagePicker.kt
51 lines (49 loc) 路 1.74 KB
/
ImagePicker.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.piashcse.compose_museum.screens
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import coil.compose.AsyncImage
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@Composable
fun ImagePicker(navController: NavController) {
var result by remember { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result = it.data?.data
}
val intent = Intent(MediaStore.ACTION_PICK_IMAGES).apply {
type = "image/*"
}
Column(
Modifier.fillMaxWidth().fillMaxHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = { launcher.launch(intent) }) {
Text(text = "Photo picker")
}
AsyncImage(
model = result,
contentDescription = "Image from photo picker",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(200.dp, 200.dp)
.clip(CircleShape)
)
}
}