2

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.

3
  • why you are not using database Commented Sep 14, 2015 at 4:15
  • But problem is related to database Commented Sep 14, 2015 at 4:17
  • @bpA sadly, as far as for now, I have no experience with database. And my app doesnt need much data for future reference either. Yes, I am new to development. Yes, i'll soon be using database, but for now, need to solve it as it is. :) Commented Sep 14, 2015 at 4:19

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Took me some time to understand serializable but finally its working now. thnanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.