Why can't I use getFilesDir(); in a static context?

11,354

Solution 1

That is because in the static method you have not got the object of the class and getFilesDir is not a static method that means it will only be accessible via the object of class Context.

So what you can do is store the reference to the object in a static variable of your class and then use that in your static method.

for example:

static YourContextClass obj;

static void method(){
   File myFile = new File (obj.getFilesDir(), filename );
}

also you will have to store the reference to the object in your onCreateMethod()

 obj = this;

The best way to achieve this is

 static void method(YourContextClass obj){
      File myFile = new File (obj.getFilesDir(), filename );
 }

Solution 2

I'm gonna talk about what happened to me. I'm developing a log system files, so i created a new class and i wanted to was for all my application and having many instances of this class doing different logs. So i thougth to create protected or public objects of my class on the application class that is similar to a singleton class.

So i had something like that:

      public class MyApp extends Application {
        protected LogApp logApp = new LogApp(getFilesDir());

When i called it from my main class to get the list files for example:

      public class LogApp {
         public File dirFiles;

         //file parameter can't be null, the app will crash
         public LogApp(File file){
            dirFiles = file;
         }

         public File[] getListFiles(){
            return dirFiles.listFiles()
         }

      public class MainActivity extends AppCompatActivity {
         protected void onCreate(Bundle savedInstanceState) {
            MyApp myApp = (MyApp)getApplicationContext();
            File file[] = myApp.logApp.getListFiles();
      }

This was getting me an nullPointException error. The solution in this case was so easy that i felt stuoid and proud at the same time.

I couldn't call to getFilesDir in MyApp's declaration space because at that moment there isn'r a context to get that Dir. The order of execute in an Android App is: Application --> Activity. Like it's said in Manifest file.

Solution? Create my object in onCreate event of my MyApp Class, it looks like this:

    public class MyApp extends Application {
        protected LogApp logApp; 

        void onCreate(){
           logApp = new LogApp(getFilesDir());

So now i can use it in my main class in the same way i did it because exist an instance of my MainActivity that extends in last instance from Context Class.

Maybe i'm wrong with may explanation and this is not what is really happening in meanings of terminology and how works android. If someone understand better than me why this works i invite you to clear up our doubts.

I hope this will help you.

Share:
11,354
jackgerrits
Author by

jackgerrits

Updated on June 26, 2022

Comments

  • jackgerrits
    jackgerrits almost 2 years

    I have looked everywhere for an answer and every time I see someone else use the method:

    getFilesDir();
    

    But when I try and use that method in any way, especially:

    File myFile = new File (getFilesDir();, filename );
    

    Eclipse just says, "Cannot make static reference to non-static method getFilesDir from tye ContextWrapper"

    I am trying to use it to get the internal directory to write a file for my application.

    Thanks!