Skip to content

Commit

Permalink
fix: 강아지 프로필 이미지 형식 Bitmap -> Uri로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
nueijeel committed Jan 5, 2024
1 parent a046b21 commit 6149adb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +29,7 @@ class PhotoSelectBottomSheetFragment : BottomSheetDialogFragment() {
lateinit var requestCameraLauncher: ActivityResultLauncher<Intent>
lateinit var requestAlbumLauncher: ActivityResultLauncher<Intent>
lateinit var file: File
lateinit var photoURI: Uri

private var photoMenuListener: PhotoMenuListener? = null

Expand Down Expand Up @@ -78,7 +77,7 @@ class PhotoSelectBottomSheetFragment : BottomSheetDialogFragment() {

// 카메라 앱 실행하는 인텐트 생성
fun executeCamera(context: Context) {
val photoURI: Uri =
photoURI =
FileProvider.getUriForFile(
context,
"com.project.meongcare",
Expand All @@ -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()
}
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,16 +11,16 @@ import javax.inject.Inject
class DogAddViewModel
@Inject
constructor() : ViewModel() {
private val _dogProfileImage = MutableLiveData<Bitmap?>()
val dogProfileImage: LiveData<Bitmap?>
private val _dogProfileImage = MutableLiveData<Uri?>()
val dogProfileImage: LiveData<Uri?>
get() = _dogProfileImage

private val _dogBirthDate = MutableLiveData<String>()
val dogBirthDate: LiveData<String>
get() = _dogBirthDate

fun getDogProfileImage(bitmap: Bitmap) {
_dogProfileImage.value = bitmap
fun getDogProfileImage(uri: Uri) {
_dogProfileImage.value = uri
}

fun getDogBirthDate(str: String) {
Expand Down

0 comments on commit 6149adb

Please sign in to comment.