0

This is how I create the file:

File directory = null;
    File file = null;

    try {

        directory = new File(this.getExternalFilesDir(null));
    } catch (Exception ex) {

        ex.printStackTrace();
        if (!directory.exists()) {
            directory.mkdirs();
        }
    }

    file = new File(directory, "user_data.json");

    if (!file.exists()) {
        try {

            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

and then this file appears in:

Android/data/com.mypackage.asd/files/user_data.json

but later on when I need it, using this code:

FileInputStream fis = null;
        try {
            fis = context.openFileInput("user_data.json");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

I get a NullPointerException and I see that the system looks for the file in

data/data/com.mypackage.asd/files/user_data.json

Why does it replace "Android" with "data" in the path?

ngrashia
  • 9,869
  • 5
  • 43
  • 58
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

1 Answers1

1

In the first case, you are using getExternalFilesDir().

In the second case, you are using openFileInput().

Those are not pointing to the same place.

If you want your file to be placed onto external storage, use getExternalFilesDir() everywhere.

If you want your file to be placed onto internal storage, use getFilesDir() and/or openFileInput().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491