简体   繁体   中英

Android: List all file in a directory

I'm trying to build an application that lists all files in a directory and allows the user tap a file and have it open in a specific app depending on the file extension. So far, I've written this code to list all the files in the directory but it doesn't seem to work - it just crashes the application.

Could anyone advise why please? I've tried to replicate other code snippets I've found on SO and other various places, but none seem to work.

Thanks in advance

String home = Environment.getExternalStorageDirectory().toString() + "/vuza/" + username;
Toast.makeText(this, "Debug: " + home, Toast.LENGTH_SHORT).show();

File root = new File(home);

File fileList[] = root.listFiles();

for(int x = 0; x < fileList.length; x++) {
    Toast.makeText(this, fileList[x].getName(), Toast.LENGTH_SHORT).show();
}

EDIT: Added stack-trace

04-08 19:29:39.230    5264-5264/com.mikekaperys.assignment E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mikekaperys.assignment, PID: 5264
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mikekaperys.assignment/com.mikekaperys.assignment.Files}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mikekaperys.assignment.Files.onCreate(Files.java:32)
            at android.app.Activity.performCreate(Activity.java:5426)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

1) Put this in manifest:

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

2) Also check if that home path exists.

3) fileList.length may return null if no directory is in there. Check if fileList is null before loop .

Replace your code with below one

String home = Environment.getExternalStorageDirectory().toString() + "/vuza/" + username;

File file = new File(home);

if(file.exists())
{
//method call
    listf(home);
}
else
{
Toast.makeText(this,"no dir extists"), Toast.LENGTH_SHORT).show();
}

public File[] listf(String directoryName) {

    // .............list file
    File directory = new File(directoryName);

    // get all the files from a directory
    File[] fList = directory.listFiles();

    for (File file : fList) {
        if (file.isFile()) {
            System.out.println(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath());
        }
    }
    System.out.println(fList);
    return fList;
} 

also make sure you have given following permission

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM