Android: How to store data on internal memory?
Asked Answered
I

4

7

It's perfectly described here how to do it, the only problem: He doesnt know the function openFileOutput();

private void saveSettingsFile() {
          String FILENAME = "settings";
          String string = "hello world!";

          FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); //openFileOutput underlined red
          try {
            fos.write(string.getBytes());
            fos.close();
          } catch (IOException e) {
            Log.e("Controller", e.getMessage() + e.getLocalizedMessage() + e.getCause());
          }
}

Those are the relevant packages I imported:

import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
Infective answered 2/9, 2010 at 14:23 Comment(6)
Can you share logcat error output?Jaffe
I just noticed that "openFileOutput" is underlined red. Eclipse asked me to create a so-named method in my class, which I accidently did. Now I removed this method stub and "openFileOutput" is underlined red again.Infective
@Konstantin: Its more a logical problem than. So logcat doesnt help yet. Right know I cant even compile it.Infective
So extend context of the code, can you post whole method?Jaffe
I extended it above. It's just a method of my classe. For my opionion this should work with just those 5 lines of code (according to the link)Infective
See ALL comments at the answer belowInfective
P
2

Have a look at this example of using a FileOutputStrem from the Examples on dev.android.com. It should give you an idea of how to use it correctly.

Poussin answered 2/9, 2010 at 14:59 Comment(12)
Thank u. I will look into it. however, I still would prefer to get this 5 lines working. I mean the author of the document I referred to above cant be fundamentally wrong.Infective
you need to call openFileOutput on a context. Try context.openFileOutput()Poussin
Ok, good suggestion. However I dont have the variable or object "context". How do I create or get it?Infective
Your context is your Activity. If this code is in a View then all views are created with a context as an argument. If this code is in another class you'll have to pass in the context as an argument as you would with a View.Poussin
In which class the method is placed? The openFileOutput should be invoked on Context instance (i.e. activity).Jaffe
@Konstantin Yup, exactly what I just said.Poussin
It's a class whose instance can be called from everywhere by its static function Controller.getInstance(). That's the singleton way of providing classes, I guess. This class provides data like the session, user data and so on. These stuff needs to be present in all activities.Infective
It's like a model for all activities through the lifetime of this app. Shall I pass the context of any Activity in its onCreate()-Method to my Controller? Doesnt that cause nasty Bugs by Threading and so on?Infective
@Infective Then you need to pass in a context instance in the method and invoke openFileInput on the instance.Jaffe
Do I have to update this reference in the controller when intenting a new activity? In my case activities dont request directly the controller to load this settings file. Model classes do that.Infective
Well you can reference application context in the controller, just pass it into getInstance method. To obtain application context you have to invoke getApplicationContext on activity.Jaffe
One more question: Is it enough to pass "getApplicationContext()" to my Controller only while creating my 1st activity? Is that passed by reference? So, is that just a pointer to the application context?Infective
A
1

Class within which this method is declared, is defined as "Static". thats why it is throwing error. Remove static from the class definition and bingo...

Anissaanita answered 16/3, 2012 at 14:14 Comment(0)
W
0

Just add a "try catch" block and put them in between this.

Like this:

    private void saveSettingsFile(String FILENAME, String data) {

    FileOutputStream fos;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } // openFileOutput underlined red
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

When there is red line under the line.. First check that the line is under the full sentense or only right side of the sentense .(i.e after equal sign).

If it covers the whole line, then it has to fix some bugs..

Or if it under only the right side of the sentense ...Then it must wants some exception handling things.

If you don't know what type of exception it may generate...
Dont fear , just write all the code in a try block( try{ } ) and then add a catch and pass a Exception object inside catch .. Now its fine..

Like this :

  try
  {
    ...........your code
    ......
  } 
   catch(Exception e)
  {
   e.printstacktrace();

  }

Now all are fine.

Thank you

Wig answered 2/8, 2013 at 12:7 Comment(0)
T
0

openFileOutput is a method of Context object. And don't forget to add finally clause to close the stream. Bellow is an example (a bit clumsy because of Java 6 because of Android).

String data = "Hello";
FileOutputStream fos = null;
try {
    fos = mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(data.getBytes(Charset.defaultCharset()));
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

mContext variable should be defined somewhere above and initialized like mContext = getApplicationContext() if you are inside of an activity

Tephra answered 22/8, 2016 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.