title: Android Studio :: Want to list files saved in default location
body: <p>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:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <p>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:</p>
  
  <pre><code>// 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;
  </code></pre>
  
  <p>And then I open "currDir," and store the names of all the local files in an array:</p>
  
  <pre><code>// 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 &lt; fileArr.length; i++) {
      fileArr[i] = filelist[i].getName();
  }
  </code></pre>
  
  <p>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":</p>
  
  <pre><code>"cache"
  "code_cache"
  "files"
  </code></pre>
  
  <p>This is true no matter how many files I've saved previously.</p>
  
  <p>BTW, in the debugger, the assignments for packName and currDir look 100% correct:</p>
  
  <pre><code>packName = com.mydomain.myapp
  currDir  = /data/user/0/com.mydomain.myapp
  </code></pre>
  
  <p>So...  I'm kinda assuming that my saved files are actually here:</p>
  
  <pre><code>/data/user/0/com.mydomain.myapp/files
  </code></pre>
  
  <p>And therefore, I should append this to my "get current directory" code:</p>
  
  <pre><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";     // &lt;---- appending "+"/files"
  </code></pre>
  
  <p>Or am I way off?  Any advice will be appreciated, thanks!</p>