Search code examples
androidandroid-file

Android 4.4 files list issue


I tried to get files in /sdcard/ folder, but it throws NullPointerException

Then I tried to get folders in / path it returns

40
factory
usbdisk
sdcard
storage
config
cache
acct
vendor
d
etc
mnt
ueventd.tuna.rc
ueventd.rc
ueventd.goldfish.rc
system
sys
sepolicy
seapp_contexts
sbin
res
... and others

When I do this in 4.3 project it works well!

UPDATE

code in 4.4 which not work

File file = Environment.getExternalStorageDirectory();
File[] files = file.listFiles();
Debugger.info(files.length); // NullPointerExcepton
for(File f : files){
    Debugger.info(f.getName());
}

Solution

  • The Android Developers site states (in the important behaviour changes in KitKat)

    If your app reads from external storage...

    Your app can not read shared files on the external storage when running on Android 4.4, unless your app has the READ_EXTERNAL_STORAGE permission. That is, files within the directory returned by getExternalStoragePublicDirectory() are no longer accessible without the permission. However, if you need to access only your app-specific directories, provided by getExternalFilesDir(), then you do not need the READ_EXTERNAL_STORAGE permission.

    So make sure you give the above permision. Also, if you are using a device with multiple sd cards you may run in this issue.