mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-24 22:45:20 +00:00
fix camera button (#572)
* fix camera button * fix * Fix: Add runtime camera permission check for image picker
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.bitchat.android.ui.media
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@@ -19,6 +21,7 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.FileProvider
|
||||
import com.bitchat.android.features.media.ImageUtils
|
||||
import java.io.File
|
||||
@@ -70,8 +73,16 @@ fun ImagePickerButton(
|
||||
)
|
||||
capturedImagePath = file.absolutePath
|
||||
takePictureLauncher.launch(uri)
|
||||
} catch (_: Exception) {
|
||||
// Ignore errors; no-op
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("ImagePickerButton", "Camera capture failed", e)
|
||||
}
|
||||
}
|
||||
|
||||
val permissionLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.RequestPermission()
|
||||
) { isGranted ->
|
||||
if (isGranted) {
|
||||
startCameraCapture()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +91,13 @@ fun ImagePickerButton(
|
||||
.size(32.dp)
|
||||
.combinedClickable(
|
||||
onClick = { imagePicker.launch("image/*") },
|
||||
onLongClick = { startCameraCapture() }
|
||||
onLongClick = {
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
startCameraCapture()
|
||||
} else {
|
||||
permissionLauncher.launch(Manifest.permission.CAMERA)
|
||||
}
|
||||
}
|
||||
),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user