Skip to content

Commit f1e1bc5

Browse files
committed
refactor: Bitmap -> byteArray -> Bitmap 저장을, Bitmap -> ByteArray로 변경 (#46 (comment))
1 parent 225c4b9 commit f1e1bc5

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@ import androidx.room.ForeignKey
2727
data class MomentPictureXRef(
2828
@ColumnInfo(name = "moment_id", index = true) val momentId: Long,
2929
@ColumnInfo(name = "picture_id", index = true) val pictureId: Long,
30-
)
31-
// PR 리뷰 남기고 -> 사진
30+
)

data/src/main/java/com/wakeup/data/util/InternalFileUtil.kt

+31-28
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import kotlinx.coroutines.withContext
1111
import timber.log.Timber
1212
import java.io.ByteArrayOutputStream
1313
import java.io.File
14-
import java.io.FileNotFoundException
1514
import java.io.FileOutputStream
16-
import java.io.IOException
1715
import java.security.DigestException
1816
import java.security.MessageDigest
1917
import javax.inject.Inject
@@ -27,23 +25,24 @@ class InternalFileUtil @Inject constructor(
2725
return withContext(dispatcher) {
2826
val pictureFiles = mutableListOf<String>()
2927
pictures.forEach { picture ->
30-
println(picture)
3128
val fileName = hashSHA256(encodeBase64(picture.bitmap))
3229
val tempFile = File(context.filesDir, fileName)
3330

34-
try {
31+
runCatching {
3532
tempFile.createNewFile()
36-
val out = FileOutputStream(tempFile)
37-
val convertedBitmap =
38-
BitmapFactory.decodeByteArray(picture.bitmap, 0, picture.bitmap.size)
39-
convertedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
40-
out.close()
41-
} catch (e: FileNotFoundException) {
33+
}.onFailure { e ->
4234
Timber.e("MyTag", "FileNotFoundException : " + e.message)
43-
} catch (e: IOException) {
44-
Timber.e("MyTag", "IOException : " + e.message)
35+
}.onSuccess {
36+
runCatching {
37+
val out = FileOutputStream(tempFile)
38+
out.write(picture.bitmap)
39+
out.close()
40+
}.onFailure { e ->
41+
Timber.e("MyTag", "IOException : " + e.message)
42+
}.onSuccess {
43+
pictureFiles.add(fileName)
44+
}
4545
}
46-
pictureFiles.add(fileName)
4746
}
4847
pictureFiles.map { PictureEntity(fileName = it) }
4948
}
@@ -53,17 +52,18 @@ class InternalFileUtil @Inject constructor(
5352
return withContext(dispatcher) {
5453
val pictureBitmaps = mutableListOf<ByteArray>()
5554

56-
val files: Array<String> = context.fileList()
5755
pictures.forEach { pictureFileName ->
58-
files.forEach { savedFileName ->
59-
Timber.d("MyTag", savedFileName)
60-
println(savedFileName)
61-
println(pictureFileName.fileName)
62-
if (savedFileName == pictureFileName.fileName) {
63-
val bitmapPicture =
64-
BitmapFactory.decodeFile("${context.filesDir}/${savedFileName}")
65-
val stream = ByteArrayOutputStream()
66-
bitmapPicture.compress(Bitmap.CompressFormat.PNG, 1, stream)
56+
runCatching {
57+
BitmapFactory.decodeFile("${context.filesDir}/${pictureFileName.fileName}")
58+
}.onFailure { e ->
59+
Timber.e("MyTag", "FileNotFoundException : " + e.message)
60+
}.onSuccess { bitmapPicture ->
61+
runCatching {
62+
ByteArrayOutputStream()
63+
}.onFailure { e ->
64+
Timber.e("MyTag", "IOException : " + e.message)
65+
}.onSuccess { stream ->
66+
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 1, stream)
6767
pictureBitmaps.add(stream.toByteArray())
6868
}
6969
}
@@ -82,22 +82,25 @@ class InternalFileUtil @Inject constructor(
8282
val md = MessageDigest.getInstance("SHA-256")
8383
md.update(msg.toByteArray())
8484
hash = md.digest()
85-
} catch (e: CloneNotSupportedException){
85+
} catch (e: CloneNotSupportedException) {
8686
throw DigestException("couldn't make digest of patial content")
8787
}
8888
return bytesToHex(hash)
8989
}
9090

91-
private val digits = "0123456789ABCDEF"
91+
private fun bytesToHex(byteArray: ByteArray): String {
9292

93-
fun bytesToHex(byteArray: ByteArray): String {
9493
val hexChars = CharArray(byteArray.size * 2)
9594
for (i in byteArray.indices) {
9695
val v = byteArray[i].toInt() and 0xff
97-
hexChars[i * 2] = digits[v shr 4]
98-
hexChars[i * 2 + 1] = digits[v and 0xf]
96+
hexChars[i * 2] = DIGITS[v shr 4]
97+
hexChars[i * 2 + 1] = DIGITS[v and 0xf]
9998
}
10099
return String(hexChars)
101100
}
101+
102+
companion object {
103+
private const val DIGITS = "0123456789ABCDEF"
104+
}
102105
}
103106

presentation/src/main/java/com/wakeup/presentation/mapper/PictureModelMapper.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import java.io.ByteArrayOutputStream
88

99
fun PictureModel.toDomain(): Picture {
1010
val stream = ByteArrayOutputStream()
11-
bitmap.compress(Bitmap.CompressFormat.PNG, 1, stream)
12-
println(stream.toByteArray())
11+
bitmap.compress(Bitmap.CompressFormat.JPEG, 1, stream)
1312
return Picture(stream.toByteArray())
1413
}
1514

0 commit comments

Comments
 (0)