Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.listFiles returns null when it shouldn't

I am trying to get list of files existing in the Phone Pictures folder (internal storage, no SDCard inserted) using this:

 File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
 File[] files = file.listFiles();

but files value is always being null. I also add this to AndroidManifest.xml

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Any ideas will be much appreciated, thanks in advance.

like image 569
Yahya Hussein Avatar asked Oct 18 '25 13:10

Yahya Hussein


2 Answers

If you are targeting Android SDK >=23 you need to take permission at run-time.

https://developer.android.com/training/permissions/requesting.html

like image 69
Abhishek Aryan Avatar answered Oct 20 '25 05:10

Abhishek Aryan


It returns null as Environment.getExternalStoragePublicDirectory will/is supposed to handle files from external storage like SD card..as you are stating no sd card is inserted

like image 23
Akshay Avatar answered Oct 20 '25 05:10

Akshay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!