diff --git a/app/src/main/java/com/project/meongcare/PhotoSelectBottomSheetFragment.kt b/app/src/main/java/com/project/meongcare/PhotoSelectBottomSheetFragment.kt index f1a74e369..fa77dd016 100644 --- a/app/src/main/java/com/project/meongcare/PhotoSelectBottomSheetFragment.kt +++ b/app/src/main/java/com/project/meongcare/PhotoSelectBottomSheetFragment.kt @@ -3,8 +3,6 @@ package com.project.meongcare import android.app.Activity import android.content.Context import android.content.Intent -import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.net.Uri import android.os.Bundle import android.os.Environment @@ -31,6 +29,7 @@ class PhotoSelectBottomSheetFragment : BottomSheetDialogFragment() { lateinit var requestCameraLauncher: ActivityResultLauncher lateinit var requestAlbumLauncher: ActivityResultLauncher lateinit var file: File + lateinit var photoURI: Uri private var photoMenuListener: PhotoMenuListener? = null @@ -78,7 +77,7 @@ class PhotoSelectBottomSheetFragment : BottomSheetDialogFragment() { // 카메라 앱 실행하는 인텐트 생성 fun executeCamera(context: Context) { - val photoURI: Uri = + photoURI = FileProvider.getUriForFile( context, "com.project.meongcare", @@ -95,13 +94,8 @@ class PhotoSelectBottomSheetFragment : BottomSheetDialogFragment() { registerForActivityResult( ActivityResultContracts.StartActivityForResult(), ) { - val option = BitmapFactory.Options() - option.inSampleSize = 5 - val bitmap = BitmapFactory.decodeFile(file.absolutePath, option) - bitmap?.let { bitmapFile -> - if (bitmapFile != null) { - sendBitmap(bitmapFile) - } + if (it.resultCode == Activity.RESULT_OK) { + sendUri(photoURI) dismiss() } } @@ -118,29 +112,19 @@ class PhotoSelectBottomSheetFragment : BottomSheetDialogFragment() { registerForActivityResult( ActivityResultContracts.StartActivityForResult(), ) { - val option = BitmapFactory.Options() - option.inSampleSize = 5 - if (it.resultCode == Activity.RESULT_OK) { it.data?.data?.let { uri -> if (uri != null) { - val inputStream = mainActivity.contentResolver.openInputStream(uri) - val bitmap = BitmapFactory.decodeStream(inputStream, null, option) - inputStream?.close() - bitmap?.let { bitmapFile -> - if (bitmapFile != null) { - sendBitmap(bitmapFile) - } - dismiss() - } + sendUri(uri) } + dismiss() } } } } - private fun sendBitmap(bitmap: Bitmap) { - photoMenuListener?.onBitmapPassed(bitmap) + private fun sendUri(uri: Uri) { + photoMenuListener?.onBitmapPassed(uri) } fun setPhotoMenuListener(listener: PhotoMenuListener) { diff --git a/app/src/main/java/com/project/meongcare/onboarding/model/data/local/PhotoMenuListener.kt b/app/src/main/java/com/project/meongcare/onboarding/model/data/local/PhotoMenuListener.kt index 2ae8ee5c6..13a29e807 100644 --- a/app/src/main/java/com/project/meongcare/onboarding/model/data/local/PhotoMenuListener.kt +++ b/app/src/main/java/com/project/meongcare/onboarding/model/data/local/PhotoMenuListener.kt @@ -1,7 +1,7 @@ package com.project.meongcare.onboarding.model.data.local -import android.graphics.Bitmap +import android.net.Uri interface PhotoMenuListener { - fun onBitmapPassed(bitmap: Bitmap) + fun onBitmapPassed(uri: Uri) } diff --git a/app/src/main/java/com/project/meongcare/onboarding/view/DogAddOnBoardingFragment.kt b/app/src/main/java/com/project/meongcare/onboarding/view/DogAddOnBoardingFragment.kt index 58ceb9859..5b0a7006e 100644 --- a/app/src/main/java/com/project/meongcare/onboarding/view/DogAddOnBoardingFragment.kt +++ b/app/src/main/java/com/project/meongcare/onboarding/view/DogAddOnBoardingFragment.kt @@ -1,14 +1,15 @@ package com.project.meongcare.onboarding.view -import android.graphics.Bitmap +import android.content.Context +import android.net.Uri import android.os.Bundle -import android.util.Base64 import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import com.bumptech.glide.Glide import com.google.android.material.chip.Chip import com.google.gson.Gson import com.project.meongcare.CalendarBottomSheetFragment @@ -27,8 +28,9 @@ import kotlinx.coroutines.runBlocking import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody +import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.toRequestBody -import java.io.ByteArrayOutputStream +import java.io.File import java.text.SimpleDateFormat import javax.inject.Inject @@ -162,36 +164,37 @@ class DogAddOnBoardingFragment : Fragment(), PhotoMenuListener, DateSubmitListen ) val json = Gson().toJson(dog) val requestBody: RequestBody = json.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull()) - val filePart = createMultipartBody(dogAddViewModel.dogProfileImage.value) + val filePart = createMultipartBody(mainActivity, dogAddViewModel.dogProfileImage.value) // 서버로 전송 runBlocking { - userPreferences.accessToken.collect { accessToken -> - if (accessToken != null) { - val dogAddResponse = dogAddRepository.postDogInfo(accessToken, filePart, requestBody) +// userPreferences.accessToken.collect { accessToken -> +// if (accessToken != null) { + val dogAddResponse = + dogAddRepository.postDogInfo( + "Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MywiZXhwIjoxNzA0NDQyOTM2fQ.NKUEOyiPWhUC5FvBrKHXy-vlo6O7D5pioUb0oULAgck", + filePart, + requestBody, + ) if (dogAddResponse == 200) { // CompleteOnBoardingFragment로 이동 } - } - } - } -// runBlocking { -// val dogAddResponse = dogAddRepository.postDogInfo(accessT, filePart, requestBody) -// if (dogAddResponse == 200) { -// // CompleteOnBoardingFragment로 이동 +// } // } -// } + } } } return fragmentDogAddOnBoardingBinding.root } - override fun onBitmapPassed(bitmap: Bitmap) { - dogAddViewModel.getDogProfileImage(bitmap) + override fun onBitmapPassed(uri: Uri) { + dogAddViewModel.getDogProfileImage(uri) fragmentDogAddOnBoardingBinding.run { - imageviewPetaddImage.setImageBitmap(bitmap) + Glide.with(this@DogAddOnBoardingFragment) + .load(uri) + .into(imageviewPetaddImage) imageviewPetaddDog.visibility = View.GONE textviewPetaddImageDescription.visibility = View.GONE } @@ -209,14 +212,21 @@ class DogAddOnBoardingFragment : Fragment(), PhotoMenuListener, DateSubmitListen return outputDateFormat.format(parsedDate) } - fun createMultipartBody(bitmap: Bitmap?): MultipartBody.Part { - if (bitmap != null) { - val byteArrayOutputStream = ByteArrayOutputStream() - bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream) - val byteArray = byteArrayOutputStream.toByteArray() - val base64Image = Base64.encodeToString(byteArray, Base64.DEFAULT) + fun createMultipartBody( + context: Context, + uri: Uri?, + ): MultipartBody.Part { + if (uri != null) { + val inputStream = context.contentResolver.openInputStream(uri) + val file = File(context.cacheDir, "tempFile") + inputStream.use { input -> + file.outputStream().use { output -> + input?.copyTo(output) + } + } + val requestFile = file.asRequestBody("multipart/form-data".toMediaTypeOrNull()) - return MultipartBody.Part.createFormData("file", "image.jpg", base64Image.toRequestBody()) + return MultipartBody.Part.createFormData("file", file.name, requestFile) } val emptyBody = "".toRequestBody("multipart/form-data".toMediaTypeOrNull()) return MultipartBody.Part.createFormData("file", "", emptyBody) diff --git a/app/src/main/java/com/project/meongcare/onboarding/viewmodel/DogAddViewModel.kt b/app/src/main/java/com/project/meongcare/onboarding/viewmodel/DogAddViewModel.kt index 05c394cc7..a4094c713 100644 --- a/app/src/main/java/com/project/meongcare/onboarding/viewmodel/DogAddViewModel.kt +++ b/app/src/main/java/com/project/meongcare/onboarding/viewmodel/DogAddViewModel.kt @@ -1,6 +1,6 @@ package com.project.meongcare.onboarding.viewmodel -import android.graphics.Bitmap +import android.net.Uri import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel @@ -11,16 +11,16 @@ import javax.inject.Inject class DogAddViewModel @Inject constructor() : ViewModel() { - private val _dogProfileImage = MutableLiveData() - val dogProfileImage: LiveData + private val _dogProfileImage = MutableLiveData() + val dogProfileImage: LiveData get() = _dogProfileImage private val _dogBirthDate = MutableLiveData() val dogBirthDate: LiveData get() = _dogBirthDate - fun getDogProfileImage(bitmap: Bitmap) { - _dogProfileImage.value = bitmap + fun getDogProfileImage(uri: Uri) { + _dogProfileImage.value = uri } fun getDogBirthDate(str: String) {