1

I am trying to make a list of all the files in my external storage but always comes back null.

if (Environment.getExternalStorageDirectory().exists()) {
        Log.i(FILE, "is not empty");

        File[] files = Environment.getExternalStorageDirectory().listFiles();

        for (File file : files) {
            Log.i("File ", file.getAbsolutePath());
        }
    }

How do I make it list all the files from the external directory

I get an exception

Caused by: java.lang.NullPointerException
                                                                                  at com.example.sam.read.fragments.MyFragment.onCreate(MyFragment.java:138)
                                                                                  at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
                                                                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
                                                                                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
                                                                                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
                                                                                  at android.app.Activity.performStart(Activity.java:5114)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                                  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                  at android.os.Looper.loop(Looper.java:137) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 
Sam321pbs
  • 301
  • 2
  • 17
  • On which line do you get the exception ? – Varun Kumar Jan 07 '16 at 17:28
  • What is line number `138` in `MyFragment.java` ? – ρяσѕρєя K Jan 07 '16 at 17:28
  • check line # 138 in onCreate() of MyFragment. Some variable is not initialized, causing NullPointerException – Shahzeb Jan 07 '16 at 17:29
  • I get the exception thrown in the for loop – Sam321pbs Jan 07 '16 at 17:34
  • It is an extension to this question I asked earlier today http://stackoverflow.com/questions/34658494/trying-to-get-a-file-path-to-a-public-folder-on-android-device-and-get-its-conte – Sam321pbs Jan 07 '16 at 17:38
  • If your `targetSdkVersion` is 23 or higher, and you are testing this on Android 6.0+, [you need to request the permission at runtime](http://stackoverflow.com/questions/32635704/cant-get-the-permission). I usually expect to see a `SecurityException` rather than a `NullPointerException` for that problem, though this may be a matter of how `listFiles()` is implemented. – CommonsWare Jan 07 '16 at 17:40

2 Answers2

1

You are missing a permission in your manifest. Add

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

to your manifest.

user3597165
  • 199
  • 2
  • 5
  • It is an extension to this question I asked earlier today http://stackoverflow.com/questions/34658494/trying-to-get-a-file-path-to-a-public-folder-on-android-device-and-get-its-conte – Sam321pbs Jan 07 '16 at 17:39
  • From de Docs: [link](http://developer.android.com/intl/es/reference/android/os/Environment.html#getExternalStorageDirectory()) ´This directory may not currently be accessible if it has been mounted by the user on their computer´ – user3597165 Jan 07 '16 at 18:09
1

The NullPointer is probably coming from for (File file : files), reason being that files is null.

From the documentation, listFiles() will return null if the specified path is not a directory; what it does not tell you is that you will most likely also get this value if you have a FileSystem permissions issue.

if ( Environment.getExternalStorageDirectory().exists() )

Do you have the required permissions defined in your manifest?

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

If this is not the issue, then my only thought is that FILE is null, or line 138 is not in this snippet.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • I have the permission and it is getting thrown on "for (File file : files)" – Sam321pbs Jan 07 '16 at 17:41
  • Can you set a breakpoint after that line? See what `files` contains. – nbokmans Jan 07 '16 at 17:42
  • He has an NPE, so `files` will be null. The question now is why `listFiles` is returning that null. – Matt Clark Jan 07 '16 at 17:43
  • This is an extension to this question I asked earlier http://stackoverflow.com/questions/34658494/trying-to-get-a-file-path-to-a-public-folder-on-android-device-and-get-its-conte – Sam321pbs Jan 07 '16 at 17:46