0

I'm new to Android Studio 3.0, emulating on a Nexus 4, Marshmallow. I'm trying to build simple "Save File" and "Load File" parts of my app. Here's the "Save File" part:

String filename = "myFile01";     // Then "myFile02", "myFile03", etc...
String userData = "Some useful data here...";
try {
    // Adapted from:  https://www.youtube.com/watch?v=_15mKw--RG0
    FileOutputStream fileOutputStream = openFileOutput(filename, MODE_PRIVATE);     // creates a file with given filename
    fileOutputStream.write(userData.getBytes());                                    // puts userData into the file
    fileOutputStream.close();
    Toast.makeText(getApplicationContext(), "File saved!", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

The above code will be called again and again as the user creates and saves additional files. Later, the user may want to view all the saved files and load one. I'll have a ListView displaying all the files... but I need help reading the current directory to get that list.

I thought I read somewhere that in Android, there's one flat directory for your app to save and retrieve files. So I was hoping if I saved a bunch of files and then called a read() method, all my saved files would simply be in the default directory, no need to search. That seems to be a bad assumption; here's why:

Here's my code looking in the default directory and listing all the files found within there. First, I need the path of said default directory:

// Get current directory adapted from:  https://stackoverflow.com/questions/5527764/get-application-directory
String packName, currDir;
PackageManager m = getPackageManager();
packName = getPackageName();
PackageInfo p = null;
try {
    p = m.getPackageInfo(packName, 0);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
currDir = p.applicationInfo.dataDir;

And then I open "currDir," and store the names of all the local files in an array:

// get list of files adapted from:  https://stackoverflow.com/questions/9317483/showing-a-list-of-files-in-a-listview#9317583
File dir = new File(currDir);
File[] filelist = dir.listFiles();
String[] fileArr = new String[filelist.length];
for (int i = 0; i < fileArr.length; i++) {
    fileArr[i] = filelist[i].getName();
}

The plan from here is to load the "fileArr" into a ListView and go from there. But when I step through the debugger, I see this as the contents of "fileArr":

"cache"
"code_cache"
"files"

This is true no matter how many files I've saved previously.

BTW, in the debugger, the assignments for packName and currDir look 100% correct:

packName = com.mydomain.myapp
currDir  = /data/user/0/com.mydomain.myapp

So... I'm kinda assuming that my saved files are actually here:

/data/user/0/com.mydomain.myapp/files

And therefore, I should append this to my "get current directory" code:

// Get current directory adapted from:  https://stackoverflow.com/questions/5527764/get-application-directory
String packName, currDir;
...everything from before...
currDir = p.applicationInfo.dataDir+"/files";     // <---- appending "+"/files"

Or am I way off? Any advice will be appreciated, thanks!

Pete
  • 1,511
  • 2
  • 26
  • 49

1 Answers1

0

First of all, if you want to save your files in the app's directory, then you should call create a directory,

File directoryDefault = new File(Environment.DIRECTORY_DOCUMENTS, "YOUR_FOLDER_NAME");
    if (!directoryDefault.exists()) {
        directoryDefault.mkdir();
    }

Then you have to save whatever files you have to save in the above mentioned default directory. Afterwards, when you want to list all the files available in that directory, you should call,

private ArrayList<String> fileNames() {
    ArrayList<String> namesArray = new ArrayList<>();
    File[] arrayFiles = directoryDefault.listFiles();

    for (File file : arrayFiles) {
        namesArray.add(file.getName());
    }
    return namesArray;
}
Pratik Mhatre
  • 779
  • 7
  • 12