Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.tudee.presentation.screen.category.component

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
Expand Down Expand Up @@ -35,6 +34,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -62,6 +62,8 @@ import com.example.tudee.presentation.screen.category.model.CategorySheetMode
import com.example.tudee.presentation.screen.category.model.CategorySheetState
import com.example.tudee.presentation.screen.category.model.UiImage
import com.example.tudee.presentation.screen.category.model.isNotNull
import com.example.tudee.utils.saveImageToInternalStorage
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand Down Expand Up @@ -115,39 +117,21 @@ private fun CategorySheetContent(
}

val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()

val photoPickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickVisualMedia()
) { uri ->
uri?.let {
try {
context.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
selectedUiImage = UiImage.External(it.toString())
Log.d("IMAGE_PICKER", "Selected image URI: $uri")
} catch (e: Exception) {
Log.e("PERMISSION", "Error: ${e.message}")
coroutineScope.launch {
val tempUri = saveImageToInternalStorage(context, uri)
selectedUiImage = UiImage.External(tempUri.toString())
}
}
}

val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
photoPickerLauncher.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
)
} else {
Log.e("PERMISSION", "Storage permission denied")
}
}

val isFormValid = categoryName.isNotBlank() && selectedUiImage.isNotNull()


Column(
modifier = Modifier
.padding(horizontal = 16.dp)
Expand All @@ -168,26 +152,9 @@ private fun CategorySheetContent(
CategoryImageSection(
selectedImage = selectedUiImage,
onEditImage = {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
when {
ContextCompat.checkSelfPermission(
context,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED -> {
photoPickerLauncher.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
)
}

else -> {
permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}
} else {
photoPickerLauncher.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
)
}
}
)
}
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/com/example/tudee/utils/SaveImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.tudee.utils

import android.content.Context
import android.net.Uri
import androidx.core.net.toUri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.util.UUID

/**
* Copies an image from a given content URI to the app's internal storage.
*
* @param context The application context.
* @param originalUri The content URI of the original image.
* @return The URI of the newly created copy, or null if it fails.
*/
suspend fun saveImageToInternalStorage(context: Context, originalUri: Uri): Uri? {
return withContext(Dispatchers.IO) {
try {
val inputStream = context.contentResolver.openInputStream(originalUri)

val fileName = "copied_image_${UUID.randomUUID()}.jpg"
val file = File(context.filesDir, fileName)
val outputStream = FileOutputStream(file)

inputStream?.copyTo(outputStream)

inputStream?.close()
outputStream.close()

file.toUri()
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}