Search code examples
androidfilefile-managementinternal-storage

Can't get path to /data/data/my_app/files


I'm working on an app which stores small amounts of data in /data/data/my_app/files using this code:

private void buildFileFromPreset(String fileName) {

    fileName = fileName.toLowerCase();

    StandardData data = StandardData.getInstance();
    String[] list = data.getDataByName(fileName);

    FileOutputStream fos = null;
    try {
        fos = openFileOutput(fileName, Context.MODE_PRIVATE);
        PrintWriter writer = new PrintWriter(fos);
        for (int i = 0; i < list.length; i++) {
            writer.println(list[i]);
        }
        writer.close();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

All works fine when the app gets started, in the onCreate() function of the main activity, I want to check if the files that were created last time are still present:

private String getAppFilesDir() {
    ContextWrapper c = new ContextWrapper(this);
    return c.getFilesDir().toString();
}

which returns something like:

/data/user/0/my_app/files

I've read some older posts (2012) suggesting this method must work but it doesn't, probably since jellybean.

So my question: How can I check if the files I created using FileOutputStream and PrintWriter in a previous session still exist?

I hope I provided enough info for you guys to answer (:


Solution

  • I still have not found a solution for this specific problem. Instead I am now using SQLite so I don't have to worry about these kinds of things.