简体   繁体   English

如何获取Android目录中文件的列表?

[英]How to get the list of files in a directory in Android?

I want to list all images in a directory to make a gallery. 我想列出目录中的所有图像以创建画廊。 The example below is working with the Intent to get the directory path. 下面的示例正在使用Intent获取目录路径。

I have tried the listFiles() method but it returns null. 我已经尝试过listFiles()方法,但是它返回null。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Permision Request
    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            1);
    initialize();
}

//For the Intent to get Folder
private void initialize() {
    Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    startActivityForResult(Intent.createChooser(i, "Choose directory"), 9999);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case 9999:
            //File myFile = new File(data.getData().toString());
            String path = data.getData().toString();
            //View the path
            Log.i("Test", "Result URI " + path);
            Toast.makeText(getApplicationContext(), "Result URI " +data.getData().toString(), Toast.LENGTH_LONG).show();

            //Creating new File for the directory
            File directory = new File(path);
            File[] files = directory.listFiles();

            Log.d("Files", "Size: "+ files.length);
            for (int i = 0; i < files.length; i++)
            {
                //Show the name of the files in the directory
                Log.d("Files", "FileName:" + files[i].getName());
            }

            break;
    }
}

in the manifest I also have the permission to read external storage 在清单中,我也有权读取外部存储

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

Is expected to return de name of the files in the directory but instead returns this error: 预期返回目录中文件的名称,但返回此错误:

Caused by: java.lang.NullPointerException: Attempt to get the length of null array
    at cu.edu.cujae.citi.folderpicker.MainActivity.workWithFile(MainActivity.java:59)
    at cu.edu.cujae.citi.folderpicker.MainActivity.onActivityResult(MainActivity.java:49)
    at android.app.Activity.dispatchActivityResult(Activity.java:6932)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

The example below is working with a Intent to get the directory path. 下面的示例正在使用Intent来获取目录路径。

No, it is using ACTION_OPEN_DOCUMENT_TREE to get a document tree. 不,它正在使用ACTION_OPEN_DOCUMENT_TREE获取文档树。 This may or may not represent a directory on the filesystem. 这可能代表文件系统上的目录,也可能不代表。

The simplest way to work with the Uri returned by ACTION_OPEN_DOCUMENT_TREE is to use DocumentFile.fromTreeUri() . 使用ACTION_OPEN_DOCUMENT_TREE返回的Uri的最简单方法是使用DocumentFile.fromTreeUri() Then, you can call listFiles() on it to get DocumentFile objects pointing to the documents (and sub-trees) within that tree. 然后,您可以对其调用listFiles()以获得指向该树中文档(和子树)的DocumentFile对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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