diff --git a/app/src/main/java/com/easyhz/picly/provider/PiclyFileProvider.kt b/app/src/main/java/com/easyhz/picly/provider/PiclyFileProvider.kt index ca0d0bd..9b15d33 100644 --- a/app/src/main/java/com/easyhz/picly/provider/PiclyFileProvider.kt +++ b/app/src/main/java/com/easyhz/picly/provider/PiclyFileProvider.kt @@ -14,20 +14,24 @@ class PiclyFileProvider: FileProvider(R.xml.file_paths) { * @return 캐시 파일 Content Uri */ suspend fun getIncomingImageUri(context: Context, uri: Uri): Uri { - val directory = File(context.cacheDir, "images") - directory.mkdirs() - val file = File.createTempFile( - "incoming_image_", - ".jpeg", - directory, - ) - context.saveImage(uri, file) - val authority = context.packageName + ".fileprovider" - return getUriForFile( - context, - authority, - file, - ) + try { + val directory = File(context.cacheDir, "images") + directory.mkdirs() + val file = File.createTempFile( + "incoming_image_", + ".jpeg", + directory, + ) + context.saveImage(uri, file) + val authority = context.packageName + ".fileprovider" + return getUriForFile( + context, + authority, + file, + ) + } catch (e: Exception) { + throw e + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/easyhz/picly/view/album/upload/UploadFragment.kt b/app/src/main/java/com/easyhz/picly/view/album/upload/UploadFragment.kt index c50cb87..3c2c8b6 100644 --- a/app/src/main/java/com/easyhz/picly/view/album/upload/UploadFragment.kt +++ b/app/src/main/java/com/easyhz/picly/view/album/upload/UploadFragment.kt @@ -51,9 +51,7 @@ import com.easyhz.picly.util.toTimeFormat import com.easyhz.picly.util.toTimeFormat24 import com.easyhz.picly.view.album.upload.gallery.GalleryBottomSheetFragment import com.easyhz.picly.view.album.upload.gallery.GalleryImageAdapter.Companion.MAX_SELECTED -import com.easyhz.picly.view.dialog.EitherDialog import com.easyhz.picly.view.dialog.LoadingDialog -import com.easyhz.picly.view.dialog.Orientation import com.easyhz.picly.view.navigation.NavControllerManager import com.google.android.material.textfield.TextInputLayout import dagger.hilt.android.AndroidEntryPoint @@ -73,9 +71,11 @@ class UploadFragment: Fragment() { private var isShowTimePicker: Boolean = false private var isGranted: Boolean = false private val args: UploadFragmentArgs by navArgs() -// private val galleryPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> -// this.isGranted = isGranted -// } + private val galleryPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> + this.isGranted = isGranted + if (isGranted) getIncomingImages() + else showGalleryPermissionDialog() + } private var bottomSheetFragment: GalleryBottomSheetFragment? = null private lateinit var getResult: ActivityResultLauncher @@ -103,7 +103,7 @@ class UploadFragment: Fragment() { } private fun setUp() { - getIncomingImages() + checkPermission() initCalendarView() initTimePicker() setTagField() @@ -121,9 +121,27 @@ class UploadFragment: Fragment() { setActivityResultLauncher() } + private fun checkPermission() { + if (args.incomingImages.isNullOrEmpty()) return + if (ContextCompat.checkSelfPermission( + requireContext(), + Manifest.permission.READ_EXTERNAL_STORAGE + ) == PackageManager.PERMISSION_GRANTED + ) { + isGranted = true + getIncomingImages() + } else { + galleryPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE) + } + } + private fun getIncomingImages() { - loading.show(true) + if (!isStoragePermissionGranted()) { + showGalleryPermissionDialog() + return + } CoroutineScope(Dispatchers.Main).launch { + loading.show(true) try { val incomingImages = args.incomingImages?.mapNotNull { PiclyFileProvider.getIncomingImageUri(requireContext(), it) @@ -132,7 +150,7 @@ class UploadFragment: Fragment() { viewModel.addSelectedImageList(it) } } catch (e: Exception) { - BlueSnackBar.make(binding.root, getString(R.string.incoming_image_error)) + BlueSnackBar.make(binding.root, getString(R.string.incoming_image_error)).show() } finally { loading.show(false) } @@ -446,31 +464,23 @@ class UploadFragment: Fragment() { setButtonTextColor(binding.expireTimeButton, isShowTimePicker) } } - private fun initViews() = with(binding) { -// if (ContextCompat.checkSelfPermission( -// requireContext(), -// Manifest.permission.READ_EXTERNAL_STORAGE -// ) == PackageManager.PERMISSION_GRANTED -// ) { -// isGranted = true -// } else { -// galleryPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE) -// } - } private fun showGalleryPermissionDialog() { - val dialog = EitherDialog.instance( - title = getString(R.string.dialog_gallery_permission_title), - message = getString(R.string.dialog_gallery_permission_message), - Orientation.VERTICAL + showAlertDialog( + context = requireContext(), + title= R.string.dialog_gallery_permission_title, + message = R.string.dialog_gallery_permission_message, + positiveButtonText = R.string.dialog_gallery_permission_positive, + onContinue = { + val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + intent.data = Uri.parse("package:" + requireActivity().packageName) + startActivity(intent) + }, + negativeButtonText = R.string.dialog_upload_negative, + onCancel = { + BlueSnackBar.make(binding.root, getString(R.string.incoming_image_error)) + } ) - dialog.setPositiveButton(getString(R.string.dialog_gallery_permission_positive), ContextCompat.getColor(requireActivity(), R.color.highlightBlue)) { - val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - intent.data = Uri.parse("package:" + requireActivity().packageName) - startActivity(intent) - }.setNegativeButton(getString(R.string.dialog_gallery_permission_negative), ContextCompat.getColor(requireActivity(), R.color.secondText)) { - - }.show(requireActivity().supportFragmentManager) } private fun showUrlDialog(url: String) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0768e9b..ef2b0dd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -83,7 +83,7 @@ 정말로 삭제하시겠습니까? 권한 설정 - 파일 및 미디어 접근을 위해 앱 설정에서 권한을 설정해주세요. + 파일 및 미디어 접근을 위해 앱 설정에서 권한을 설정해 주세요. 앱 설정 창 닫기 @@ -147,4 +147,6 @@ +버튼을 눌러 앨범을 업로드하고 공유해보세요. 이미지를 불러오는데 실패하였습니다. + 앱 설정에서 권한을 설정해 주세요. + \ No newline at end of file