0

I am looking to get the file path of a folder and then get its content. The folders are a public folder that you can access by just connecting the device into the computer. How do I go about getting a the file path to get access to these public folders and then get all its content?

So once I connect my computer to the device these are the folders I see and there is one that says "Folder to get items from" and I can't seem to figure what the file path is in order to get access to its content enter image description here.

This is how I'm trying to get its content and Log them

    File fileOfEpubs = new File("/data/data/Folder of to get items from/");


    File[] dirEpub = fileOfEpubs.listFiles();

    if (dirEpub.length != 0){
        for (int i = 0; i < dirEpub.length; i++){
            String fileName = dirEpub[i].toString();
            Log.i("File", "File name = " + fileName);
        }
    }
Sam321pbs
  • 301
  • 2
  • 17

2 Answers2

0

You appear to be looking at the root of external storage. Programmatically, you access that via Environment.getExternalStorageDirectory(). That requires either the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • When I try using Environment.getExternalStorageDirectory() and try getting the list of all the files I get a nullPointerException. Code you show me an example of how you would do it? – Sam321pbs Jan 07 '16 at 16:57
  • @Sam321pbs: "When I try using Environment.getExternalStorageDirectory() and try getting the list of all the files I get a nullPointerException" -- open a fresh Stack Overflow question where you show your current code, provide the stack trace, and indicate what line(s) in the code are referenced in the stack trace. "Code you show me an example of how you would do it?" -- given your existing code, `File[] dirEpub=Environment.getExternalStorageDirectory();`, I guess. I don't know what you're trying to do. However, note that `listFiles()` returns `null` if the `File` is not pointing to a directory – CommonsWare Jan 07 '16 at 17:00
  • http://stackoverflow.com/questions/34661450/trying-to-list-all-folders-files-from-internal-sdcard-android-but-get-a-null – Sam321pbs Jan 07 '16 at 17:29
  • There is the question. Appreciate the help. – Sam321pbs Jan 07 '16 at 17:30
-1

You may need to escape the spaces in your string

File fileOfEpubs = new File("/data/data/Folder\ of\ to\ get\ items\ from/");
Matt Clark
  • 27,671
  • 19
  • 68
  • 123