Thursday, March 26, 2015

How to save/store object in Android

Method 1: Save Object as JSON String in SharedPreference

Use GSON Library: It can be used to convert Object in to JSON representation and JSON string to equivalent object.



//To Save
SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);
Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

//To Retreive
Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);

Method 2: Serializing your object to a private file

Sample Below

//Read Object from File
String devStatusPath = getApplicationContext().getFilesDir() +File.separator +DeviceStatus.class.getSimpleName();
Log.i("[ TEST ]", "ONCREATE PATH = "+devStatusPath);

File file = new File(devStatusPath);

if(file.exists()){
 mDeviceStatus = (DeviceStatus) Serialize
   .readObjectFromFile(getApplicationContext(),
     DeviceStatus.class.getSimpleName());
}

//Write to File
Serialize.witeObjectToFile(getApplicationContext(), mDeviceStatus, DeviceStatus.class.getSimpleName());

//Serialize.java

package com.android.manifesttest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.content.Context;

public class Serialize {

    public static void witeObjectToFile(Context context, Object object, String filename) {

        ObjectOutputStream objectOut = null;
        try {

            FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE);
            objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(object);
            fileOut.getFD().sync();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectOut != null) {
                try {
                    objectOut.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static Object readObjectFromFile(Context context, String filename) {
        ObjectInputStream objectIn = null;
        Object object = null;
        try {

            FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
            objectIn = new ObjectInputStream(fileIn);
            object = objectIn.readObject();

        } catch (FileNotFoundException e) {
            // Do nothing
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectIn != null) {
                try {
                    objectIn.close();
                } catch (IOException e) {
                }
            }
        }
        return object;
    }
}

0 comments:

Post a Comment