Skip to content
Snippets Groups Projects
Commit 622453d5 authored by Altair1618's avatar Altair1618
Browse files

fix: update json writing and pretty print

parent 89240420
...@@ -54,9 +54,6 @@ dependencies { ...@@ -54,9 +54,6 @@ dependencies {
implementation("androidx.camera:camera-extensions:${cameraxVersion}") implementation("androidx.camera:camera-extensions:${cameraxVersion}")
// JSON // JSON
// implementation("com.squareup.moshi:moshi:1.15.1")
// implementation("com.squareup.moshi:moshi-adapters:1.15.1")
// implementation("com.squareup.moshi:moshi-kotlin:1.15.1")
implementation("com.google.code.gson:gson:2.10.1") implementation("com.google.code.gson:gson:2.10.1")
// OpenCV // OpenCV
......
...@@ -8,7 +8,10 @@ import android.net.Uri ...@@ -8,7 +8,10 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Environment import android.os.Environment
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.k2_9.omrekap.data.models.ImageSaveData import com.k2_9.omrekap.data.models.ImageSaveData
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
...@@ -23,6 +26,8 @@ import java.util.Locale ...@@ -23,6 +26,8 @@ import java.util.Locale
* Helper class for saving images and JSON data * Helper class for saving images and JSON data
*/ */
object SaveHelper { object SaveHelper {
private val gson = GsonBuilder().setPrettyPrinting().create()
/** /**
* Save the image and JSON data to the device * Save the image and JSON data to the device
* @param context the application context * @param context the application context
...@@ -200,7 +205,7 @@ object SaveHelper { ...@@ -200,7 +205,7 @@ object SaveHelper {
val jsonFile = File(fileDir, fileName) val jsonFile = File(fileDir, fileName)
val fileOutputStream = FileOutputStream(jsonFile) val fileOutputStream = FileOutputStream(jsonFile)
fileOutputStream.write(data.toString().toByteArray()) fileOutputStream.write(gson.toJson(data).toByteArray())
fileOutputStream.flush() fileOutputStream.flush()
fileOutputStream.close() fileOutputStream.close()
} }
...@@ -226,13 +231,13 @@ object SaveHelper { ...@@ -226,13 +231,13 @@ object SaveHelper {
if (uri != null) { if (uri != null) {
val stream = context.contentResolver.openOutputStream(uri) val stream = context.contentResolver.openOutputStream(uri)
if (stream != null) { if (stream != null) {
stream.write(data.toString().toByteArray()) stream.write(gson.toJson(data).toByteArray())
stream.close() stream.close()
} else { } else {
// TODO: Handle the case where the stream is null Log.e("SaveHelper", "Failed to open stream")
} }
} else { } else {
// TODO: Handle the case where the uri is null Log.e("SaveHelper", "Failed to create uri")
} }
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment