@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)
@Composable
fun MyContent1(navController: NavController, leaveViewModel: LeaveViewModel)
{
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
val empID = userViewModel.getSFCode()
//FOR PERMISSION
val launcherMultiplePermissions = rememberLauncherForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { mapResults ->
mapResults.forEach {
Log.d("Permissions", "Permission: ${it.key} Status: ${it.value}")
}
// check if any of the requested permissions is granted or not
if (mapResults.values.all { it })
{
Constant.showToast(context, "Permission Granted")
}
else
{
Constant.showToast(context, "Permission Denied, Please give the permission to access..!")
}
}
Column(
Modifier
.fillMaxSize()
.background(colorResource(id = R.color.backgroundColor))
.padding(start = 10.dp, end = 10.dp)
.verticalScroll(state = rememberScrollState()), // Add verticalScroll modifier
horizontalAlignment = Alignment.CenterHorizontally
) {
// Function to validate the selected file size
fun isFileSizeValid(context: Context, uri: Uri): Boolean {
val inputStream: InputStream? = context.contentResolver.openInputStream(uri)
inputStream?.use { stream ->
val fileSize = stream.available().toLong()
return fileSize <= MAX_FILE_SIZE_BYTES
}
return false
}
val contentResolver = LocalContext.current.contentResolver
val getContent = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
if (uri != null) {
val mimeType = contentResolver.getType(uri)
if (mimeType != null && (mimeType.startsWith("image/") || mimeType == "application/pdf"))
{
// Check if the selected file size is valid
if (isFileSizeValid(context, uri))
{
selectedUri.value = uri // Handle the returned URI
leaveFileName.value = getFileName(uri,context)
leaveFilePath.value = leaveFileName.value
Log.d("Apply Leave New","File size is valid. Proceed with processing.")
}
else
{
Constant.showToast(context, "File size exceeds the maximum allowed size.")
Log.d("Apply Leave New","File size exceeds the maximum allowed size.")
}
}
else
{
Constant.showToast(context,"Please select an image or PDF file")
}
}
}
Column(modifier = Modifier.padding(10.dp).weight(1f)){
Button(onClick = {
filePathClearance()
val granted = checkAndRequestStoragePermissions(
context = context,
launcher = launcherMultiplePermissions
)
Log.d("granted value", "$granted")
if(granted)
{ getContent.launch("*/*") }
},
modifier = Modifier.fillMaxSize(),
colors = ButtonDefaults.buttonColors(colorResource(id = R.color.themeColor)))
{
Text(
text = leaveFileName.value.take(13) + "..." ,
style = TextStyle(
fontFamily = poppins_font,
fontSize = 13.sp,
fontWeight = FontWeight(500),
color = colorResource(id = R.color.white)
),
modifier = Modifier.tooltipTrigger()
)
}
}
}
}
//To check permissions
fun checkAndRequestStoragePermissions(
context: Context,
launcher: ManagedActivityResultLauncher<Array, Map<String, Boolean>>
): Boolean {
val accessStorage: Array<String> = // Permission request logic
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU){
arrayOf(android.Manifest.permission.READ_MEDIA_IMAGES)
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE){
arrayOf(android.Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED, android.Manifest.permission.READ_MEDIA_IMAGES)//READ_MEDIA_VISUAL_USER_SELECTED
} else {
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
val permissionsToRequest: MutableList<String> = mutableListOf()
if (!checkInViewModel.arePermissionsGranted(context, accessStorage))
{
permissionsToRequest.addAll(accessStorage)
}
if (permissionsToRequest.isNotEmpty()) {
launcher.launch(permissionsToRequest.toTypedArray())
}
return permissionsToRequest.isEmpty()
}
I want to access the photos and file for upload, but in my app, if i select “allow all” option, then it works fine, but when i select “Allow limited access” then, it display the message as “permission granted” and again ask for permission, and then app is crashed with “media played keeps stopping” when select “Allow limited access” again.