diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a0d2079cb042af49a7c244c4c5de753672e516d2..59bbfcd07ddf4775c0da584845d192628400c8a9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -54,9 +54,6 @@ dependencies { implementation("androidx.camera:camera-extensions:${cameraxVersion}") // 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") // OpenCV diff --git a/app/src/main/java/com/k2_9/omrekap/utils/SaveHelper.kt b/app/src/main/java/com/k2_9/omrekap/utils/SaveHelper.kt index e612364b09b79a49c8ac4dd25e15ab1e4cb30eda..89754c65cb376116a333a67958de71b5e09c5685 100644 --- a/app/src/main/java/com/k2_9/omrekap/utils/SaveHelper.kt +++ b/app/src/main/java/com/k2_9/omrekap/utils/SaveHelper.kt @@ -8,7 +8,9 @@ import android.net.Uri import android.os.Build import android.os.Environment import android.provider.MediaStore +import android.util.Log import androidx.annotation.RequiresApi +import com.google.gson.GsonBuilder import com.k2_9.omrekap.data.models.ImageSaveData import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -23,6 +25,8 @@ import java.util.Locale * Helper class for saving images and JSON data */ object SaveHelper { + private val gson = GsonBuilder().setPrettyPrinting().create() + /** * Save the image and JSON data to the device * @param context the application context @@ -200,7 +204,7 @@ object SaveHelper { val jsonFile = File(fileDir, fileName) val fileOutputStream = FileOutputStream(jsonFile) - fileOutputStream.write(data.toString().toByteArray()) + fileOutputStream.write(gson.toJson(data).toByteArray()) fileOutputStream.flush() fileOutputStream.close() } @@ -226,13 +230,13 @@ object SaveHelper { if (uri != null) { val stream = context.contentResolver.openOutputStream(uri) if (stream != null) { - stream.write(data.toString().toByteArray()) + stream.write(gson.toJson(data).toByteArray()) stream.close() } else { - // TODO: Handle the case where the stream is null + Log.e("SaveHelper", "Failed to open stream") } } else { - // TODO: Handle the case where the uri is null + Log.e("SaveHelper", "Failed to create uri") } } }