From 72f33360e1b81af0fc3e8d00ece140fba79ef698 Mon Sep 17 00:00:00 2001 From: kimdoyun98 Date: Thu, 6 Mar 2025 19:00:31 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[Feat]=20=EC=95=A8=EB=B2=94=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EC=B7=A8=EC=86=8C=20=EC=8B=9C=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../add/savephoto/navigation/SaveAlbumNavigation.kt | 5 ++++- .../naturealbum/ui/navigation/NatureAlbumState.kt | 11 +++++++++-- .../com/and04/naturealbum/utils/image/ImageConvert.kt | 7 ++++++- gradle.properties | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt index 94c2388b..9be4962a 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt @@ -26,7 +26,10 @@ fun NavGraphBuilder.saveAlbumNavGraph( location = state.lastLocation.value, model = state.imageUri.value, fileName = state.fileName.value, - onBack = { state.takePicture(takePictureLauncher) }, + onBack = { + state.deleteFilePhoto() + state.takePicture(takePictureLauncher) + }, onSave = { navigator.navigateSavePhotoToAlbum() state.selectedLabel.value = null diff --git a/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt b/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt index 36a0f010..944517a0 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt @@ -40,8 +40,11 @@ class NatureAlbumState( navigator: NatureAlbumNavigator ) { if (result.resultCode == RESULT_OK) { - val resizePicture = ImageConvert.resizeImage(imageUri.value)!! - imageFile.value?.delete() + val resizePicture = ImageConvert.resizeImage(imageUri.value) { file -> + imageFile.value?.delete() + imageFile.value = file + }!! + imageUri.value = resizePicture.uri fileName.value = resizePicture.fileName @@ -75,6 +78,10 @@ class NatureAlbumState( } } } + + fun deleteFilePhoto() { + imageFile.value?.delete() + } } @Composable diff --git a/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt b/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt index 39c7b855..22ef0615 100644 --- a/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt +++ b/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt @@ -22,7 +22,10 @@ object ImageConvert { private const val COMPRESS_QUALITY = 80 private const val IN_SAMPLE_SIZE = 16 - fun resizeImage(uri: Uri): ResizePicture? { + fun resizeImage( + uri: Uri, + changeFile: (File) -> Unit, + ): ResizePicture? { try { val context = NatureAlbum.getInstance() val storage = context.filesDir @@ -45,6 +48,8 @@ object ImageConvert { fos.flush() } + changeFile(imageFile) + return ResizePicture( fileName = fileName, uri = FileProvider.getUriForFile( diff --git a/gradle.properties b/gradle.properties index e0d20494..e2c5a9fa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,4 +21,4 @@ kotlin.code.style=official # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true -android.experimental.androidTest.useUnifiedTestPlatform=false +#android.experimental.androidTest.useUnifiedTestPlatform=false From fe2aef013876abd4859abcd0f27c614e00fadea8 Mon Sep 17 00:00:00 2001 From: kimdoyun98 Date: Thu, 6 Mar 2025 20:21:02 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[Style]=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt b/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt index 944517a0..63012e08 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt @@ -59,7 +59,6 @@ class NatureAlbumState( } fun takePicture(launcher: ManagedActivityResultLauncher) { - // TODO: imageUri가 EMPTY가 아닐때 해당 파일 삭제 fileName.value = "temp_${System.currentTimeMillis()}.jpg" imageFile.value = File(context.filesDir, fileName.value) imageUri.value = From 51ff0a47e54d853862cb5185d347aa846abede60 Mon Sep 17 00:00:00 2001 From: kimdoyun98 Date: Fri, 7 Mar 2025 20:17:02 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[Refactor]=20=EC=A0=80=EC=9E=A5=20=EC=A0=84?= =?UTF-8?q?=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=BA=90=EC=8B=9C=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workmanager/SynchronizationWorker.kt | 29 +------- .../ui/add/savephoto/SavePhotoScreen.kt | 27 +------ .../add/savephoto/SavePhotoScreenLandscape.kt | 12 +-- .../add/savephoto/SavePhotoScreenPortrait.kt | 12 +-- .../navigation/SaveAlbumNavigation.kt | 6 +- .../ui/navigation/NatureAlbumState.kt | 27 +++---- .../naturealbum/utils/image/ImageConvert.kt | 73 ++++++++++++++----- app/src/main/res/xml/file_paths.xml | 4 + 8 files changed, 90 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/com/and04/naturealbum/background/workmanager/SynchronizationWorker.kt b/app/src/main/java/com/and04/naturealbum/background/workmanager/SynchronizationWorker.kt index d3e1db0b..9232250b 100644 --- a/app/src/main/java/com/and04/naturealbum/background/workmanager/SynchronizationWorker.kt +++ b/app/src/main/java/com/and04/naturealbum/background/workmanager/SynchronizationWorker.kt @@ -268,8 +268,7 @@ class SynchronizationWorker @AssistedInject constructor( val valueList = fileNameToLabelUid.values.toList() val findAlbumData = valueList.find { value -> value.second == photo.fileName } - val uri = makeFileToUri(photo.uri, photo.fileName) - + val uri = ImageConvert.makeFileToUri(photo.uri, photo.fileName, true) val labelId = findAlbumData?.first ?: fileNameToLabelUid[photo.label]?.first @@ -383,32 +382,6 @@ class SynchronizationWorker @AssistedInject constructor( } } - private fun makeFileToUri(photoUri: String, fileName: String): String { - val context = applicationContext - val storage = context.filesDir - val imageFile = File(storage, fileName) - imageFile.createNewFile() - - FileOutputStream(imageFile).use { fos -> - BitmapFactory.decodeStream(URL(photoUri).openStream()).apply { - if (Build.VERSION.SDK_INT >= 30) { - compress(Bitmap.CompressFormat.WEBP_LOSSY, 100, fos) - } else { - compress(Bitmap.CompressFormat.JPEG, 100, fos) - } - - recycle() - } - fos.flush() - } - - return FileProvider.getUriForFile( - context, - "${context.packageName}.fileprovider", - imageFile - ).toString() - } - private fun isUnSyncLabel(label: SyncAlbumsDto, firebaseLabel: FirebaseLabelResponse): Boolean { return (firebaseLabel.labelName == label.labelName) && ((firebaseLabel.fileName != label.fileName) diff --git a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreen.kt b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreen.kt index e2765aa6..518e178a 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreen.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreen.kt @@ -5,8 +5,6 @@ import android.content.Context import android.content.Intent import android.content.res.Configuration.UI_MODE_NIGHT_NO import android.content.res.Configuration.UI_MODE_NIGHT_YES -import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.location.Location import android.net.Uri import androidx.activity.compose.BackHandler @@ -80,7 +78,6 @@ import com.and04.naturealbum.utils.network.NetworkState import com.and04.naturealbum.utils.network.NetworkState.DISCONNECTED import com.google.firebase.auth.ktx.auth import com.google.firebase.ktx.Firebase -import java.io.IOException import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -89,7 +86,6 @@ fun SavePhotoScreen( locationHandler: LocationHandler, location: Location?, model: Uri, - fileName: String, onBack: () -> Unit, onSave: () -> Unit, onCancel: () -> Unit, @@ -141,7 +137,6 @@ fun SavePhotoScreen( SavePhotoScreen( model = model, - fileName = fileName, location = newLocation, photoSaveState = photoSaveState, rememberDescription = rememberDescription, @@ -165,7 +160,6 @@ fun SavePhotoScreen( @Composable fun SavePhotoScreen( model: Uri, - fileName: String, location: State, rememberDescription: State, onDescriptionChange: (String) -> Unit, @@ -200,8 +194,7 @@ fun SavePhotoScreen( if (context.isPortrait()) { SavePhotoScreenPortrait( innerPadding = innerPadding, - model = model, - fileName = fileName, + uri = model, label = label, location = location.value!!, rememberDescription = rememberDescription, @@ -216,8 +209,7 @@ fun SavePhotoScreen( } else { SavePhotoScreenLandscape( innerPadding = innerPadding, - model = model, - fileName = fileName, + uri = model, label = label, location = location.value!!, rememberDescription = rememberDescription, @@ -381,19 +373,9 @@ fun Description( } } -private fun loadImageFromUri(context: Context, uri: Uri): Bitmap? { - return try { - context.contentResolver.openInputStream(uri)?.use { inputStream -> - BitmapFactory.decodeStream(inputStream) - } - } catch (e: IOException) { - null - } -} - fun insertFirebaseService( context: Context, - model: Uri, + uri: String, fileName: String, label: Label, location: Location, @@ -403,7 +385,7 @@ fun insertFirebaseService( if (Firebase.auth.currentUser == null || NetworkState.getNetWorkCode() == DISCONNECTED) return val newTime = time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) val intent = Intent(context, FirebaseInsertService::class.java).apply { - putExtra(SERVICE_URI, model.toString()) + putExtra(SERVICE_URI, uri) putExtra(SERVICE_FILENAME, fileName) putExtra(SERVICE_LABEL, label) putExtra(SERVICE_LOCATION_LATITUDE, location.latitude) @@ -428,7 +410,6 @@ private fun ScreenPreview() { SavePhotoScreen( model = "".toUri(), location = location, - fileName = "fileName.jpg", rememberDescription = rememberDescription, onDescriptionChange = { }, isRepresented = isRepresented, diff --git a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenLandscape.kt b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenLandscape.kt index 952cdb9a..a11a02fa 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenLandscape.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenLandscape.kt @@ -26,14 +26,14 @@ import coil3.request.crossfade import com.and04.naturealbum.R import com.and04.naturealbum.data.localdata.room.Label import com.and04.naturealbum.ui.utils.UiState +import com.and04.naturealbum.utils.image.ImageConvert import java.time.LocalDateTime import java.time.ZoneId @Composable fun SavePhotoScreenLandscape( innerPadding: PaddingValues, - model: Uri, - fileName: String, + uri: Uri, label: Label?, location: Location, rememberDescription: State, @@ -55,7 +55,7 @@ fun SavePhotoScreenLandscape( ) { AsyncImage( model = ImageRequest.Builder(context) - .data(model) + .data(uri) .crossfade(true) .build(), contentDescription = stringResource(R.string.save_photo_screen_image_description), @@ -108,8 +108,10 @@ fun SavePhotoScreenLandscape( stringRes = R.string.save_photo_screen_save, onClick = { val time = LocalDateTime.now(ZoneId.of("UTC")) + val fileName = "${System.currentTimeMillis()}.jpg" + val fileUri = ImageConvert.makeFileToUri(uri.toString(), fileName) savePhoto( - model.toString(), + fileUri, fileName, label!!, location, @@ -120,7 +122,7 @@ fun SavePhotoScreenLandscape( insertFirebaseService( context = context, - model = model, + uri = fileUri, fileName = fileName, label = label, location = location, diff --git a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenPortrait.kt b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenPortrait.kt index 3dd6645e..0a482535 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenPortrait.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenPortrait.kt @@ -26,14 +26,14 @@ import coil3.request.crossfade import com.and04.naturealbum.R import com.and04.naturealbum.data.localdata.room.Label import com.and04.naturealbum.ui.utils.UiState +import com.and04.naturealbum.utils.image.ImageConvert import java.time.LocalDateTime import java.time.ZoneId @Composable fun SavePhotoScreenPortrait( innerPadding: PaddingValues, - model: Uri, - fileName: String, + uri: Uri, label: Label?, location: Location, rememberDescription: State, @@ -51,7 +51,7 @@ fun SavePhotoScreenPortrait( ) { AsyncImage( model = ImageRequest.Builder(context) - .data(model) + .data(uri) .crossfade(true) .build(), contentDescription = stringResource(R.string.save_photo_screen_image_description), @@ -98,8 +98,10 @@ fun SavePhotoScreenPortrait( stringRes = R.string.save_photo_screen_save, onClick = { val time = LocalDateTime.now(ZoneId.of("UTC")) + val fileName = "${System.currentTimeMillis()}.jpg" + val fileUri = ImageConvert.makeFileToUri(uri.toString(), fileName) savePhoto( - model.toString(), + fileUri, fileName, label!!, location, @@ -110,7 +112,7 @@ fun SavePhotoScreenPortrait( insertFirebaseService( context = context, - model = model, + uri = fileUri, fileName = fileName, label = label, location = location, diff --git a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt index 9be4962a..f6ea2694 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt @@ -25,14 +25,14 @@ fun NavGraphBuilder.saveAlbumNavGraph( locationHandler = state.locationHandler.value, location = state.lastLocation.value, model = state.imageUri.value, - fileName = state.fileName.value, onBack = { - state.deleteFilePhoto() + state.deleteCachePhoto() state.takePicture(takePictureLauncher) - }, + }, onSave = { navigator.navigateSavePhotoToAlbum() state.selectedLabel.value = null + state.deleteCachePhoto() }, onCancel = { navigator.navigateToHome() }, label = state.selectedLabel.value, diff --git a/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt b/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt index 63012e08..5b4c395d 100644 --- a/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt +++ b/app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt @@ -30,9 +30,8 @@ class NatureAlbumState( var lastLocation = mutableStateOf(null) var locationHandler = mutableStateOf(LocationHandler(context)) var imageUri = mutableStateOf(Uri.EMPTY) - var fileName = mutableStateOf("") var selectedLabel = mutableStateOf(null) - private var imageFile = mutableStateOf(null) + private var imageCache = mutableStateOf(null) var currentUid = mutableStateOf("") fun handleLauncher( @@ -40,13 +39,11 @@ class NatureAlbumState( navigator: NatureAlbumNavigator ) { if (result.resultCode == RESULT_OK) { - val resizePicture = ImageConvert.resizeImage(imageUri.value) { file -> - imageFile.value?.delete() - imageFile.value = file - }!! - - imageUri.value = resizePicture.uri - fileName.value = resizePicture.fileName + ImageConvert.resizeImage(imageUri.value) { file, uri -> + imageCache.value?.delete() + imageCache.value = file + imageUri.value = uri + } locationHandler.value.getLocation { location -> lastLocation.value = location } @@ -59,13 +56,13 @@ class NatureAlbumState( } fun takePicture(launcher: ManagedActivityResultLauncher) { - fileName.value = "temp_${System.currentTimeMillis()}.jpg" - imageFile.value = File(context.filesDir, fileName.value) + val fileName = "temp_${System.currentTimeMillis()}" + imageCache.value = File.createTempFile(fileName, ".jpg", context.cacheDir) imageUri.value = FileProvider.getUriForFile( context, "${context.packageName}.fileprovider", - imageFile.value!! + imageCache.value!! ) Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply { @@ -78,8 +75,8 @@ class NatureAlbumState( } } - fun deleteFilePhoto() { - imageFile.value?.delete() + fun deleteCachePhoto() { + imageCache.value?.delete() } } @@ -91,7 +88,6 @@ fun rememberNatureAlbumState( save = { state -> mapOf( "imageUri" to state.imageUri.value.toString(), - "fileName" to state.fileName.value, "lastLocation" to state.lastLocation.value, ) }, @@ -100,7 +96,6 @@ fun rememberNatureAlbumState( context = context, ).apply { imageUri.value = Uri.parse(restoredMap["imageUri"] as String) - fileName.value = restoredMap["fileName"] as String lastLocation.value = restoredMap["lastLocation"] as Location? } } diff --git a/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt b/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt index 22ef0615..d56aff94 100644 --- a/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt +++ b/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt @@ -9,12 +9,14 @@ import android.os.Build import android.util.Base64 import android.util.Log import androidx.core.content.FileProvider +import androidx.core.net.toUri import androidx.exifinterface.media.ExifInterface import com.and04.naturealbum.NatureAlbum import java.io.BufferedInputStream import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream +import java.net.URL object ImageConvert { private const val MAX_WIDTH = 800 @@ -22,17 +24,58 @@ object ImageConvert { private const val COMPRESS_QUALITY = 80 private const val IN_SAMPLE_SIZE = 16 + fun makeFileToUri(photoUri: String, fileName: String, external: Boolean = false): String { + val context = NatureAlbum.getInstance() + val storage = context.filesDir + val imageFile = File(storage, fileName) + imageFile.createNewFile() + + FileOutputStream(imageFile).use { fos -> + if (external) { + BitmapFactory.decodeStream(URL(photoUri).openStream()).apply { + if (Build.VERSION.SDK_INT >= 30) { + compress(Bitmap.CompressFormat.WEBP_LOSSY, COMPRESS_QUALITY, fos) + } else { + compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fos) + } + + recycle() + } + } else { + val bufferedInputStream = BufferedInputStream( + context.contentResolver.openInputStream(photoUri.toUri()) + ) + + BitmapFactory.decodeStream(bufferedInputStream).apply { + if (Build.VERSION.SDK_INT >= 30) { + compress(Bitmap.CompressFormat.WEBP_LOSSY, COMPRESS_QUALITY, fos) + } else { + compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fos) + } + + recycle() + } ?: throw NullPointerException() + } + + fos.flush() + } + + return FileProvider.getUriForFile( + context, + "${context.packageName}.fileprovider", + imageFile + ).toString() + } + fun resizeImage( uri: Uri, - changeFile: (File) -> Unit, - ): ResizePicture? { + changeFile: (File, Uri) -> Unit, + ) { try { val context = NatureAlbum.getInstance() - val storage = context.filesDir - val fileName = "${System.currentTimeMillis()}.jpg" - - val imageFile = File(storage, fileName) - imageFile.createNewFile() + val storage = context.cacheDir + val fileName = "${System.currentTimeMillis()}" + val imageFile = File.createTempFile(fileName, ".jpg", storage) FileOutputStream(imageFile).use { fos -> decodeBitmapFromUri(context = context, uri = uri)?.apply { @@ -48,22 +91,17 @@ object ImageConvert { fos.flush() } - changeFile(imageFile) - - return ResizePicture( - fileName = fileName, - uri = FileProvider.getUriForFile( + changeFile( + imageFile, + FileProvider.getUriForFile( context, "${context.packageName}.fileprovider", imageFile ) ) - } catch (e: Exception) { Log.e("ImageConvert", "FileUtil - ${e.message}") } - - return null } private fun decodeBitmapFromUri(context: Context, uri: Uri): Bitmap? { @@ -158,8 +196,3 @@ object ImageConvert { } } } - -data class ResizePicture( - val fileName: String, - val uri: Uri, -) diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml index 0ce1a13f..61729361 100644 --- a/app/src/main/res/xml/file_paths.xml +++ b/app/src/main/res/xml/file_paths.xml @@ -3,4 +3,8 @@ + + From 681e6c7f6c143ce2213bff35e6fe0ab1ff59c235 Mon Sep 17 00:00:00 2001 From: kimdoyun98 Date: Fri, 7 Mar 2025 21:38:17 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[Refactor]=20=EC=BD=94=EB=93=9C=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naturealbum/utils/image/ImageConvert.kt | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt b/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt index d56aff94..50a51e99 100644 --- a/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt +++ b/app/src/main/java/com/and04/naturealbum/utils/image/ImageConvert.kt @@ -31,30 +31,22 @@ object ImageConvert { imageFile.createNewFile() FileOutputStream(imageFile).use { fos -> - if (external) { - BitmapFactory.decodeStream(URL(photoUri).openStream()).apply { - if (Build.VERSION.SDK_INT >= 30) { - compress(Bitmap.CompressFormat.WEBP_LOSSY, COMPRESS_QUALITY, fos) - } else { - compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fos) - } - - recycle() + BitmapFactory.decodeStream( + if (external) { + URL(photoUri).openStream() + } else { + BufferedInputStream( + context.contentResolver.openInputStream(photoUri.toUri()) + ) + } + ).apply { + if (Build.VERSION.SDK_INT >= 30) { + compress(Bitmap.CompressFormat.WEBP_LOSSY, COMPRESS_QUALITY, fos) + } else { + compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fos) } - } else { - val bufferedInputStream = BufferedInputStream( - context.contentResolver.openInputStream(photoUri.toUri()) - ) - - BitmapFactory.decodeStream(bufferedInputStream).apply { - if (Build.VERSION.SDK_INT >= 30) { - compress(Bitmap.CompressFormat.WEBP_LOSSY, COMPRESS_QUALITY, fos) - } else { - compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fos) - } - recycle() - } ?: throw NullPointerException() + recycle() } fos.flush()