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
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<queries>
<package android:name="com.google.android.apps.maps" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import android.text.InputFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -47,12 +49,25 @@ class LeaveReviewFragment : Fragment() {

private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
launchGalleryIntent()
launchPhotoPicker()
} else {
showToast("사진 업로드를 위해 사진 권한을 허용해 주세요.")
}
}

private val pickMedia: ActivityResultLauncher<PickVisualMediaRequest>? =
if (Build.VERSION.SDK_INT >= 33) {
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
uri?.let {
vm.addImageUri(it, onFailure = {
requireContext().showToast(getString(R.string.leave_review_max_image_toast))
})
}
}
} else {
null
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -173,24 +188,32 @@ class LeaveReviewFragment : Fragment() {

binding.addImageButton.setOnClickListener {
requestPermission(onGranted = {
launchGalleryIntent()
launchPhotoPicker()
})
}
}

private fun requestPermission(onGranted: () -> Unit) {
val permission = if (Build.VERSION.SDK_INT >= 33) Manifest.permission.READ_MEDIA_IMAGES else Manifest.permission.READ_EXTERNAL_STORAGE
if (Build.VERSION.SDK_INT >= 33) {
onGranted()
return
}
val permission = Manifest.permission.READ_EXTERNAL_STORAGE
if (ContextCompat.checkSelfPermission(requireActivity(), permission) == PackageManager.PERMISSION_GRANTED) {
onGranted()
} else {
requestPermissionLauncher.launch(permission)
}
}

private fun launchGalleryIntent() {
val intent = Intent(Intent.ACTION_PICK)
.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
galleryLauncher.launch(intent)
private fun launchPhotoPicker() {
if (Build.VERSION.SDK_INT >= 33) {
pickMedia!!.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
} else {
val intent = Intent(Intent.ACTION_PICK)
.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
galleryLauncher.launch(intent)
}
}

companion object {
Expand Down
Loading