Android:列出目录中的所有文件

时间:2015-04-08 18:23:54

标签: java android list file directory

我正在尝试构建一个列出目录中所有文件的应用程序,并允许用户点击文件并根据文件扩展名在特定应用中打开它。到目前为止,我已经编写了这段代码来列出目录中的所有文件,但它似乎没有用 - 它只是崩溃了应用程序。

有人可以提出建议吗?我试图复制我在SO和其他各个地方发现的其他代码段,但似乎都没有。

提前致谢

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();
}

编辑:添加了堆栈跟踪

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)

2 个答案:

答案 0 :(得分:1)

1)将其放入清单:

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

2)同时检查home路径是否存在。

3) fileList.length如果没有目录,则可能返回null。在fileList之前检查loop是否为空。

答案 1 :(得分:1)

用下面的代码替换你的代码

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

文件档案=新档案(家);

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;
} 

还要确保您已获得以下许可

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