diff --git a/presentation/src/main/java/org/gdsc/presentation/login/SignUpCompleteFragment.kt b/presentation/src/main/java/org/gdsc/presentation/login/SignUpCompleteFragment.kt index 1feb6791..99ea6150 100644 --- a/presentation/src/main/java/org/gdsc/presentation/login/SignUpCompleteFragment.kt +++ b/presentation/src/main/java/org/gdsc/presentation/login/SignUpCompleteFragment.kt @@ -1,40 +1,26 @@ package org.gdsc.presentation.login -import android.Manifest import android.annotation.SuppressLint -import android.content.Context import android.content.Intent -import android.content.pm.PackageManager -import android.net.Uri -import android.os.Build import android.os.Bundle -import android.provider.Settings import android.view.LayoutInflater import android.view.MotionEvent import android.view.View import android.view.ViewGroup -import android.widget.Toast -import androidx.activity.result.contract.ActivityResultContracts import androidx.cardview.widget.CardView -import androidx.core.content.ContextCompat import androidx.core.net.toUri -import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.setFragmentResultListener import androidx.navigation.fragment.findNavController -import androidx.navigation.fragment.navArgs -import com.bumptech.glide.Glide -import com.google.android.material.dialog.MaterialAlertDialogBuilder import okhttp3.MediaType import okhttp3.MultipartBody import okhttp3.RequestBody import org.gdsc.presentation.BaseFragment import org.gdsc.presentation.databinding.FragmentSignUpCompleteBinding +import org.gdsc.presentation.utils.BitmapUtils.getCompressedBitmapFromUri +import org.gdsc.presentation.utils.BitmapUtils.saveBitmapToFile import org.gdsc.presentation.utils.checkMediaPermissions -import org.gdsc.presentation.utils.findPath -import org.gdsc.presentation.utils.showMediaPermissionsDialog import org.gdsc.presentation.view.MainActivity -import java.io.File class SignUpCompleteFragment : BaseFragment() { @@ -79,15 +65,22 @@ class SignUpCompleteFragment : BaseFragment() { binding.nextBtn.setOnClickListener { viewModel.profileImageState.value.let { - if(it.isNullOrEmpty()) + if(it.isEmpty()) viewModel.requestSignUpWithoutImage { moveToMain() } else { - val file = File(it.toUri().findPath(requireContext())) - val requestFile = RequestBody.create(MediaType.parse("image/png"), file) - val body = - MultipartBody.Part.createFormData("profileImg", file.name, requestFile) - viewModel.requestSignUpWithImage(body) { - moveToMain() + val file = it.toUri() + .getCompressedBitmapFromUri(requireContext()) + ?.saveBitmapToFile(requireContext(), "profile.jpg") + + if (file != null) { + val requestFile = RequestBody.create(MediaType.parse("image/png"), file) + val body = + MultipartBody.Part.createFormData("profileImg", file.name, requestFile) + viewModel.requestSignUpWithImage(body) { + moveToMain() + } + } else { + // TODO: Exception Handling } } } diff --git a/presentation/src/main/java/org/gdsc/presentation/utils/BitmapUtils.kt b/presentation/src/main/java/org/gdsc/presentation/utils/BitmapUtils.kt new file mode 100644 index 00000000..25eaa081 --- /dev/null +++ b/presentation/src/main/java/org/gdsc/presentation/utils/BitmapUtils.kt @@ -0,0 +1,94 @@ +package org.gdsc.presentation.utils + +import android.content.ContentResolver +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Matrix +import android.net.Uri +import android.os.Environment +import androidx.exifinterface.media.ExifInterface +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream + +object BitmapUtils { + + fun Bitmap.saveBitmapToFile(context: Context, fileName: String): File { + val directory = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + directory?.mkdirs() + val file = File(directory, fileName) + + var outputStream: FileOutputStream? = null + try { + outputStream = FileOutputStream(file) + this.compress(Bitmap.CompressFormat.JPEG, 90, outputStream) + outputStream.flush() + this.recycle() + return file + } catch (e: IOException) { + e.printStackTrace() + } finally { + outputStream?.close() + } + + return file + } + + private fun Bitmap.rotateBitmap(angle: Float): Bitmap { + val matrix = Matrix() + matrix.postRotate(angle) + return Bitmap.createBitmap( + this, 0, 0, this.width, this.height, matrix, true + ) + } + + private fun getOrientationOfImage(inputStream: InputStream): Int { + val exif = try { + ExifInterface(inputStream) + } catch (e: IOException) { + e.printStackTrace() + return -1 + } + val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1) + if (orientation != -1) { + when (orientation) { + ExifInterface.ORIENTATION_ROTATE_90 -> return 90 + ExifInterface.ORIENTATION_ROTATE_180 -> return 180 + ExifInterface.ORIENTATION_ROTATE_270 -> return 270 + } + } + return 0 + } + + fun Uri.getCompressedBitmapFromUri(context: Context): Bitmap? { + val resolver: ContentResolver = context.contentResolver + var inputStream: InputStream? = null + try { + + val options = BitmapFactory.Options().apply { + inSampleSize = 2 + } + + inputStream = resolver.openInputStream(this) + + val bitmap = requireNotNull(BitmapFactory.decodeStream(inputStream, null, options)) + + val orientation = getOrientationOfImage(inputStream!!).toFloat() + + return bitmap.rotateBitmap(orientation) + } catch (e: Exception) { + e.printStackTrace() + } finally { + inputStream?.close() + } + return null + } + +} + + + + + diff --git a/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/RegisterRestaurantFragment.kt b/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/RegisterRestaurantFragment.kt index f5397fc9..cb689751 100644 --- a/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/RegisterRestaurantFragment.kt +++ b/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/RegisterRestaurantFragment.kt @@ -25,17 +25,17 @@ import org.gdsc.domain.Empty import org.gdsc.presentation.BaseFragment import org.gdsc.presentation.R import org.gdsc.presentation.databinding.FragmentRegisterRestaurantBinding +import org.gdsc.presentation.utils.BitmapUtils.getCompressedBitmapFromUri +import org.gdsc.presentation.utils.BitmapUtils.saveBitmapToFile import org.gdsc.presentation.utils.addAfterTextChangedListener import org.gdsc.presentation.utils.repeatWhenUiStarted import org.gdsc.presentation.utils.animateShrinkWidth import org.gdsc.presentation.utils.checkMediaPermissions -import org.gdsc.presentation.utils.findPath import org.gdsc.presentation.view.MainActivity import org.gdsc.presentation.view.WebViewActivity import org.gdsc.presentation.view.custom.FoodCategoryBottomSheetDialog import org.gdsc.presentation.view.restaurantregistration.adapter.RegisterRestaurantAdapter import org.gdsc.presentation.view.restaurantregistration.viewmodel.RegisterRestaurantViewModel -import java.io.File @AndroidEntryPoint class RegisterRestaurantFragment : BaseFragment() { @@ -218,20 +218,28 @@ class RegisterRestaurantFragment : BaseFragment() { if (navArgs.targetRestaurantId == -1) { val pictures = mutableListOf() - list.forEach { + list.forEachIndexed { index, sUri -> - val file = File(it.toUri().findPath(requireContext())) + sUri.toUri() + .getCompressedBitmapFromUri(context) + ?.saveBitmapToFile(context, "$index.jpg")?.let { imageFile -> - val requestFile = - RequestBody.create(MediaType.parse("image/png"), file) - val body = - MultipartBody.Part.createFormData( - "pictures", - file.name, - requestFile - ) + val requestFile = + RequestBody.create( + MediaType.parse("image/png"), + imageFile + ) + + val body = + MultipartBody.Part.createFormData( + "pictures", + imageFile.name, + requestFile + ) - pictures.add(body) + pictures.add(body) + + } } diff --git a/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/viewmodel/RegisterRestaurantViewModel.kt b/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/viewmodel/RegisterRestaurantViewModel.kt index a22a0056..0f8d70ee 100644 --- a/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/viewmodel/RegisterRestaurantViewModel.kt +++ b/presentation/src/main/java/org/gdsc/presentation/view/restaurantregistration/viewmodel/RegisterRestaurantViewModel.kt @@ -166,9 +166,7 @@ class RegisterRestaurantViewModel @Inject constructor( _foodCategoryState.value = FoodCategoryItem(FoodCategory.fromName(it.category)) _drinkPossibilityState.value = it.canDrinkLiquor _recommendDrinkTextState.value = it.goWellWithLiquor - _recommendMenuListState.value = it.recommendMenu.split("#").drop(1).apply { - println(" isRecommendMenuFullState 테스트 초기화 : ${this}") - } + _recommendMenuListState.value = it.recommendMenu.split("#").drop(1) _introductionTextState.value = it.introduce _restaurantPlaceName.value = it.name _restaurantLocationId.value = restaurantId.toString()