Code: Select all
permissionLauncher.launch(permissionToRequest)
val permissionToRequest = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Manifest.permission.READ_MEDIA_IMAGES
} else {
Manifest.permission.READ_EXTERNAL_STORAGE
}
val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { isGranted: Boolean ->
if (isGranted) {
imagePickerLauncher.launch("image/*")
}
}
)
Code: Select all
val imagePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetMultipleContents(),
onResult = { uris: List ->
// handle images.
}
)
Code: Select all
val parsedUri = imageUri.toUri() // Using androidx.core.net.toUri
var tempFile: File? = null
try {
// Use androidContext which is now the correctly typed Android Context
tempFile = File.createTempFile("exif_image_", ".jpg", androidContext.cacheDir)
AppLogger.d(tag, "Created temp file: ${tempFile.absolutePath}")
androidContext.contentResolver.openInputStream(parsedUri)?.use { inputStream ->
FileOutputStream(tempFile).use { outputStream ->
inputStream.copyTo(outputStream)
AppLogger.d(tag, "Copied content to temp file: ${tempFile.absolutePath}")
}
} ?: run {
AppLogger.e(
tag,
"Failed to open input stream for URI: $imageUri. " +
"Temp file: ${tempFile.absolutePath}"
)
return@withContext null // Early exit; finally will clean up tempFile
}
val exifInterface =
ExifInterface(tempFile.absolutePath) // Using androidx.exifinterface.media.ExifInterface
val latLong = exifInterface.latLong
if (latLong == null || (latLong.size < 2) || (latLong[0] == 0.0 && latLong[1] == 0.0)) {
AppLogger.d(tag, "No valid LatLong found in EXIF data for ${tempFile.absolutePath}")
return@withContext null
}