0

I know this question has been answered several times over, but no answers seem to help. I've tried using multiple approaches, and most recently this:

Getting the filenames of all files in a folder

which also redirects to another thread with code that would not work for me either.

My app keeps crashing, and the error says it's a null pointer.

I tried checking for a directory

    File dir = new File("C:\\Users\\myName\\files");

    //Log.d("filePath:", filePath);

    if(dir.isDirectory()) {
       Log.d()
    }

but there was no log from within my if statement. Apparently it's not being take as a directory. I must be overlooking something incredibly simple.

Community
  • 1
  • 1
Mkz
  • 1
  • 2
  • 6
  • C:\\Users\\myName\\files is not a directory on your Android device/emulator. That is, your device/emulator does not have a C: drive. – Mike M. Mar 26 '17 at 00:55
  • Wow, I feel dumb. Thank you. I'm trying to allow user input for a directory and then it is scanned. Must it be within the android, and what generally would that look like? – Mkz Mar 26 '17 at 01:00

1 Answers1

0

you can use this API

    Environment.getExternalStorageDirectory()// /storage/sdcard0

File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "ur directory name");

            if (!folder.exists()) {
                Log.e("Not Found Dir", "Not Found Dir  ");
            } else {
                Log.e("Found Dir", "Found Dir  " );
               Toast.makeText(getApplicationContext(),"Directory is already exist" ,Toast.LENGTH_SHORT).show();
            }

If you want to get information in detail, visit the following link

https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

https://gist.github.com/granoeste/5574148

redAllocator
  • 725
  • 6
  • 12
  • If a user wanted to specify the directory, on or off the device, what would that look like? – Mkz Mar 26 '17 at 01:24
  • I want the user to be able to enter a directory name and have my app find the files within that folder. If I needed to do that within the device, what would an example of a directory look like? – Mkz Mar 26 '17 at 01:31
  • Hope this helps you http://stackoverflow.com/questions/20324155/get-filepath-and-filename-of-selected-gallery-image-in-android – redAllocator Mar 26 '17 at 01:33
  • It does somewhat. I'm just confused about what a pathname looks like inside of the emulator. I don't really know where the root is and how I should call it and its subfolders. – Mkz Mar 26 '17 at 01:43
  • give me a source code link(github). so I can help you. – redAllocator Mar 26 '17 at 01:43
  • I don't use github, but let me explain it more simply. The user is prompted with a blank textfield where they enter a folder name. The app scans that folder for files (.txts specifically) and retrieves their names to store in an array. – Mkz Mar 26 '17 at 01:50
  • ok. you can get folder list something like ArrayList or String[ ] this list have folder name right? and what do you want to do? – redAllocator Mar 26 '17 at 02:06
  • I want to know what the path would look like if I were to search a local folder insider the emulator; since that's what I'll be testing it on. So the user wants to search /home/files/texts, or something. What would that path actually look like? – Mkz Mar 26 '17 at 02:10