-
-
Save virendersran01/d44c797dea81c32fcea1967715192ef2 to your computer and use it in GitHub Desktop.
New Photo Picker on Android 13/API Level 33 - Backwards Compatible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coil | |
// implementation("io.coil-kt:coil-compose:2.2.2") | |
import android.util.Log | |
import android.widget.Toast | |
import androidx.activity.compose.rememberLauncherForActivityResult | |
import androidx.activity.result.PickVisualMediaRequest | |
import androidx.activity.result.contract.ActivityResultContracts | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material.Button | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.unit.dp | |
import coil.compose.AsyncImage | |
import coil.request.ImageRequest | |
@Composable | |
fun MainScreen() { | |
val context = LocalContext.current | |
var imageUri: Any? by remember { mutableStateOf(R.drawable.picker) } | |
val photoPicker = rememberLauncherForActivityResult( | |
contract = ActivityResultContracts.PickVisualMedia() | |
) { | |
if (it != null) { | |
Log.d("PhotoPicker", "Selected URI: $it") | |
imageUri = it | |
} else { | |
Log.d("PhotoPicker", "No media selected") | |
} | |
} | |
// val multiplePhotoPicker = rememberLauncherForActivityResult( | |
// contract = ActivityResultContracts.PickMultipleVisualMedia(maxItems = 2) | |
// ) { | |
// | |
// } | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
horizontalAlignment = Alignment.CenterHorizontally, | |
verticalArrangement = Arrangement.Center | |
) { | |
AsyncImage( | |
modifier = Modifier | |
.size(250.dp) | |
.clickable { | |
photoPicker.launch( | |
PickVisualMediaRequest( | |
ActivityResultContracts.PickVisualMedia.ImageOnly | |
) | |
) | |
}, | |
model = ImageRequest.Builder(LocalContext.current) | |
.data(imageUri) | |
.crossfade(enable = true) | |
.build(), | |
contentDescription = "Avatar Image", | |
contentScale = ContentScale.Crop, | |
) | |
Spacer(modifier = Modifier.height(24.dp)) | |
Button(onClick = { | |
Toast.makeText( | |
context, | |
ActivityResultContracts.PickVisualMedia.isPhotoPickerAvailable().toString(), | |
Toast.LENGTH_LONG | |
).show() | |
}) { | |
Text(text = "Availability") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment