javaandroidgsoninternal-storage

Android Studio Gson to Internal Storage


Hello im a beginner in Android Studio, I have a problem with saving an Object to a file.

First I use the Gson libary to make the Object 'Appdata' to an JSON String and I save this String in the file Appdata.txt

Here is my InternalStorage.java class

package com.example.fragment.AppData.Logic;

import android.content.Context;

import com.example.fragment.UserInterface.MainActivity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.google.gson.Gson;

public class InternalStorage {

    private static InternalStorage instance = null;
    private static Gson gson = new Gson();

    public boolean saveData(AppData saveData) {

        gson.toJson(saveData);
        String s = gson.toJson(saveData);

        FileOutputStream fOut = null;
        try {
            fOut = MainActivity.getInstance().getContext().openFileOutput("appdata.txt", Context.MODE_PRIVATE);
            fOut.write(s.getBytes());
            fOut.close();
        } catch (Exception e) {
           return false;
        }
        return true;
    }

    public AppData loadData() {

        FileInputStream fis = null;
        try {
            fis = MainActivity.getInstance().getContext().openFileInput("appdata.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while (true) {
            try {
                if (((line = bufferedReader.readLine()) != null))
                    sb.append(line+"\n");
                break;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        String s = sb.toString();
        AppData newData = null;
        try {
            newData = gson.fromJson(s, AppData.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return newData;
    }


}

I use this method in the Appdata Class :

  public AppData loadData() {
        return internalStorage.loadData();
    }

    public boolean saveAppData() {
       return internalStorage.saveData(this);
    }

Here I use the Appdata in the MainActivity. (I don't think its neccessary to post all 3 Classes)

  appData.loadData();
    //change something in AppData
    if(!appData.saveAppData()){
        //Inform the User about the not working Save 
    }

The Problem is that changes are not saved.


Solution

  • As I see you are beginner in Android development not just in Android studio. Do not make an Activity as a Singleton. About the question. If you wanna just save an object android provide a SharedPreferences https://developer.android.com/training/data-storage/shared-preferences

    But if you really need to create a file I think you just forgot to get access to permissions.