Srujan Barai
Srujan Barai

Reputation: 2365

Android: Saving arrayList to file not working

I have an activity DrinkWater that contains recyclerView. I am trying to save the arrayList named data to file in Internal storage for future reference but somehow it isn't working.

I did add the permissions in the Manifest file so thats not the problem.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Below is the (Simplified) code for the DrinkWater.java file. I made few Toasts for debugging purpose. Therefore do not miss the //comments

    public class DrinkWater extends ActionBarActivity {

Toolbar toolbar;
private RecyclerView recyclerView;
private InfoTodayAdapter adapter;
FileOutputStream fos;
FileInputStream fis;
List<InformationToday> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drink_water);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview_today);
    adapter = new InfoTodayAdapter(this,getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

public List<InformationToday> getData(){
    try {
        fis = getBaseContext().openFileInput("arrayListToday");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //Below Toast just for debugging. 
        Toast.makeText(this,"Try block for read data called 1",Toast.LENGTH_SHORT).show(); 
        data = (List<InformationToday>) ois.readObject(); // Cause of the error. 
        //Below toast for debugging. But it is never executed.
        Toast.makeText(this,"Try block for read data called 2",Toast.LENGTH_SHORT).show();  
        ois.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.d("TAG A","arrayListToday file not found");
    } catch (Exception e){
        Log.d("TAG A","Exception generated 1");
    }
    return data;
}


public void addWater(View view) {   // This method adds data to the recyclerView. It is called by on click of the Floating Action Button
    InformationToday i = new InformationToday();
    if(view.getId() == R.id.fifty_button)
    {
        i.volume = "50";
        Log.d("TAG A","i.volume = 50");
    }
    else
    {
        i.volume = "100";
    }
    data.add(0,i);          
    adapter.update(data);   //This method is described in recyclerView adapter. It updates the recyclerView data.
    try {
        fos = getBaseContext().openFileOutput("arrayListToday",Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Toast.makeText(this,"Try block for wrtie data called 1",Toast.LENGTH_SHORT).show(); //For debugging.
        oos.writeObject(data); // Cause of the error. 
        Toast.makeText(this,"Try block for write data called 2",Toast.LENGTH_SHORT).show(); //For debugging. It is never executed.
        oos.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(this,"Catch: File not gound exception",Toast.LENGTH_SHORT).show();
        Log.d("TAG A", "Catch: File not found");
        e.printStackTrace();
    }catch (Exception e){
        Toast.makeText(this,"Catch: Exception e",Toast.LENGTH_SHORT).show();
        Log.d("TAG A","Catch: Exception e");
    }
    com.getbase.floatingactionbutton.FloatingActionsMenu f = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
    f.collapseImmediately();

}
}

No errors as such. BUT Probable exceptions from Logcat: "unknown error (code 14) could not open database." 2. "Reading a null string not supported here" 3."column context_id is not unique (code 19)"

SO question for resolving error (code 14) could not open database didnt help.

Upvotes: 2

Views: 516

Answers (1)

CodePhobia
CodePhobia

Reputation: 1315

You would have to serialize the objects and then write them into the file.

Saving (w/o exception handling code):

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();

Try this out and let me know if this works for you.

Reference Link

Upvotes: 3

Related Questions