Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
}
like image 665
Ozik Abdullaev Avatar asked Nov 26 '13 15:11

Ozik Abdullaev


People also ask

How do I get a list of audio files in a specific folder on Android?

id. mylist); myList = new ArrayList<String>(); File directory = Environment. getExternalStorageDirectory(); file = new File( directory + "/Test" ); File list[] = file. listFiles(); for( int i=0; i< list.

How do I list files in internal storage on Android?

Head to Settings > Storage > Other and you'll have a full list of all the files and folders on your internal storage. (If you'd prefer this file manager be more easily accessible, the Marshmallow File Manager app will add it as an icon to your home screen.)

How do I access files in Android Studio?

In Android Studio 3.5. 3, the Device File Explorer can be found in View -> Tool Windows. It can also be opened using the vertical tabs on the right-hand side of the main window.


1 Answers

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.

like image 100
Angelo Avatar answered Oct 12 '22 20:10

Angelo