简体   繁体   中英

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.

I have tried the listFiles() method but it returns 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.

No, it is using ACTION_OPEN_DOCUMENT_TREE to get a 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() . Then, you can call listFiles() on it to get DocumentFile objects pointing to the documents (and sub-trees) within that tree.

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