@@ -11,9 +11,7 @@ import kotlinx.coroutines.withContext
11
11
import timber.log.Timber
12
12
import java.io.ByteArrayOutputStream
13
13
import java.io.File
14
- import java.io.FileNotFoundException
15
14
import java.io.FileOutputStream
16
- import java.io.IOException
17
15
import java.security.DigestException
18
16
import java.security.MessageDigest
19
17
import javax.inject.Inject
@@ -27,23 +25,24 @@ class InternalFileUtil @Inject constructor(
27
25
return withContext(dispatcher) {
28
26
val pictureFiles = mutableListOf<String >()
29
27
pictures.forEach { picture ->
30
- println (picture)
31
28
val fileName = hashSHA256(encodeBase64(picture.bitmap))
32
29
val tempFile = File (context.filesDir, fileName)
33
30
34
- try {
31
+ runCatching {
35
32
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 ->
42
34
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
+ }
45
45
}
46
- pictureFiles.add(fileName)
47
46
}
48
47
pictureFiles.map { PictureEntity (fileName = it) }
49
48
}
@@ -53,17 +52,18 @@ class InternalFileUtil @Inject constructor(
53
52
return withContext(dispatcher) {
54
53
val pictureBitmaps = mutableListOf<ByteArray >()
55
54
56
- val files: Array <String > = context.fileList()
57
55
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)
67
67
pictureBitmaps.add(stream.toByteArray())
68
68
}
69
69
}
@@ -82,22 +82,25 @@ class InternalFileUtil @Inject constructor(
82
82
val md = MessageDigest .getInstance(" SHA-256" )
83
83
md.update(msg.toByteArray())
84
84
hash = md.digest()
85
- } catch (e: CloneNotSupportedException ){
85
+ } catch (e: CloneNotSupportedException ) {
86
86
throw DigestException (" couldn't make digest of patial content" )
87
87
}
88
88
return bytesToHex(hash)
89
89
}
90
90
91
- private val digits = " 0123456789ABCDEF "
91
+ private fun bytesToHex ( byteArray : ByteArray ): String {
92
92
93
- fun bytesToHex (byteArray : ByteArray ): String {
94
93
val hexChars = CharArray (byteArray.size * 2 )
95
94
for (i in byteArray.indices) {
96
95
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 ]
99
98
}
100
99
return String (hexChars)
101
100
}
101
+
102
+ companion object {
103
+ private const val DIGITS = " 0123456789ABCDEF"
104
+ }
102
105
}
103
106
0 commit comments