androidselectgetjsonfile-managergetfiles

How to open my app documents directory with Android File Manager


I've been trying to figure out a way to open the Android File Manager directly in my app's Documents directory so the user can select a JSON file among several without requiring the user to go search for the file path /storage/emulated/0/Android/data/com.company.app/files/Documents/. So far, I can make the "go find it yourself" tactic work, but not the "take the user to the directory for them" approach. Here's what I've tried:

// this is the "go find it yourself" approach that I've used:
String filename = this.getResources().getString(R.string.ExportImportFileNameString);
File directory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
Uri dirPathUri = Uri.fromFile(directory);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("*/*");
Intent.createChooser(intent, "Open in...");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, directory);
startActivityForResult(intent, IMPORT_REQUEST);

When my onActivityComplete handler is called for IMPORT_REQUEST I see the returned data looks like dat=content://com.lge.filemanager.FileProvider/storage/emulated/0/Android/data/com.company.app/files/Documents/SelectedFile.json flg=0x1 }

I've tried to invoke two different combinations of intent.setDataAndType instead of intent.setTypefollowing and that fails to let me select anything:

// This setDataAndType setup does not allow the user to open File Manager, nor navigate to the app Documents:
intent.setDataAndType(dirPath2, "application/json");
// This allows opening of File Manager but returns immediately without allowing the user to select a file, and returns a null data pointer:
intent.setDataAndType(dirPath2, "*/*");

Note that I've tried creating the intent object with ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT, and ACTION_VIEW with the similar result. If I only have one file, I know I can have the app simply open a stream reader for a known file name as such:

File directory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File importFile = new File(directory, filename);
try
{
   fis = new FileInputStream (importFile);
   InputStreamReader inputStreamReader =
           new InputStreamReader(fis, StandardCharsets.UTF_8);
   ...

However, that doesn't allow me the flexibility that I desire to allow a user to select from multiple files. Can anyone illuminate what's going on here and how to correct the situation.


Solution

  • Based on Commonsware feedback, here's what I have for the solution to this question:

    //--------------
    // Get the documents location for this app
    File directory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
    File desiredFilePath = new File(directory.toString());
    
    try{
        // read all pathnames for files and directory
        File[] allFilesInDirectory = desiredFilePath.listFiles();
    
        // prepare array to place all path strings into
        ArrayList<String> filesInDirectoryArray = new ArrayList<String>();
    
        // retrieve each pathname file array
        // but someone could simply use the "path" object instead
        for(File path:allFilesInDirectory){
            if (path != null){
                // put all file paths into string array
                filesInDirectoryArray.add(path.getPath());
                // could discriminate based on file extension, if so desired
            }
        }
    
        // send file path array for processing
        ProcessDocuments(filesInDirectoryArray);
    }catch(SecurityException  e){
        e.printStackTrace();
    }