0

I want to get folder names inside the special directory.

for example : inside /storage/sdcard/DCIM there is two sub folder name 100ANDRO and Camera

How can i achieve this ?

i tried :

ArrayList<File> files;
File directory = new File("/storage/sdcard/DCIM");

    File[] fList = directory.listFiles();

    for (File file : fList) {
        if (file.isDirectory()) {
            files.add(file);
        }
    } 

but i get NullPointerException

behrooz
  • 617
  • 9
  • 30

2 Answers2

1

Always use Environment.getExternalStorageDirectory().getAbsoluteFile() instead of /storage/sdcard Try this :

File extStore = Environment.getExternalStorageDirectory();
String SD_PATH = extStore.getAbsolutePath()+"DCIM";

You need permissions to read from sdcard:

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Jas
  • 3,207
  • 2
  • 15
  • 45
0

you need to init files ArrayList<File> files = new ArrayList<File>(); and yes Use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) for directory path

Sulabh Deep Puri
  • 314
  • 1
  • 13