2

I'm trying to save a random hello world file onto the internal storage of my android. When I run my app, it simply stops.

Code:

public void saveDataOnDevice(String toWrite, String filename) {
    Context ctx = null;
    try {
        FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        fos.write(toWrite.getBytes());
        fos.close();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

Another question is, what Context means. I read a lot about this here, but I still don't get it.

Community
  • 1
  • 1
arnoutvh
  • 141
  • 1
  • 3
  • 13
  • I think it can hardly be put any simplier that it was in link you provided: As the name suggests, its the context of current state of the application/object. – notanormie Apr 07 '15 at 14:35
  • 1
    Also this code is semantically incorrect as you call method on a null object which will surely throw null pointer exception. You should get the context from activity/service/application calling this method. – notanormie Apr 07 '15 at 14:36
  • show us the code on how you are calling this method – Arlind Hajredinaj Apr 07 '15 at 14:40

1 Answers1

0

First of all... your context is null always: Context ctx = null;, so you wont be able to make it work in any way.

To simplify: Context, in a simple app, will be your MainActivity. In a bigger app, you can have several contexts and answer will be more complex.

Back to your code, if it is placed inside your MainActivity context will be this:

public void saveDataOnDevice(String toWrite, String filename) {
    try {
        FileOutputStream fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
        //                     ^ --> refers to main class, in this case your MainActivity
        fos.write(toWrite.getBytes());
        fos.close();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

NOTE: if you don't have this method inside the MainActivity, you must pass a reference of your Activity to the method or the class somehow (attribute, method param, etc..etc...).

Check Android Developers::saving files for further info.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Hi @ernie379 I've seen **you have asked various questions but you have not marked any as accepted**. If this or any other answer has solved your question please consider [reviewing your questions](http://stackoverflow.com/users/3657959/ernie379?tab=questions) and [mark as accepted](http://meta.stackexchange.com/q/5234/179419) that ones that solved your doubts by clicking the check-mark. This indicates to the wider community that you've found a solution and **gives some reputation to both the answerer and yourself**. Of course, there is no obligation to do this. – Jordi Castilla Apr 07 '15 at 14:56
  • So I just initiate the Context as `Context ctx = null;` in my MainActivity? And these permissions in my Manifest, in between the activity tags? – arnoutvh Apr 07 '15 at 15:35
  • nope! Context IS your MainActivity class itself, thats why you use `this` as context **if method is inside MainActivity.java** – Jordi Castilla Apr 07 '15 at 15:36
  • ok, my app doesn't crash anymore now. Where should this file be saved? It's not in the `/data` directory – arnoutvh Apr 07 '15 at 15:44
  • [Check this answer](http://stackoverflow.com/questions/4926027/what-file-system-path-is-used-by-androids-context-openfileoutput) – Jordi Castilla Apr 07 '15 at 15:46
  • I'm an android noob, so I don't know how to display the result of the `getFilesDir()` method. I tried using textViews, which wasn't a succes. – arnoutvh Apr 07 '15 at 16:06
  • `Log.d("YOUR_TAG", "Directory = " + getFilesDir());` or `System.out.println("directory = " + getFilesDir());` – Jordi Castilla Apr 07 '15 at 16:07