How to show the list of all file names in the SD card directory of Android device?

122 Views Asked by At

In my Android native Java project, I have a class, where I wanted to show the log for all the SD card directories and file names inside those folders. My Android OS version:

Android 14 (API level 34)

Here's the code:

public class LogFileListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_file_list);

        // Get the root directory of the SD card
        File rootDirectory = Environment.getExternalStorageDirectory();

        // Log all files in the root directory
        logFiles(rootDirectory);

        // Log files in the "Podcasts" folder
        logFilesInPodcasts();

    }


    private void logFiles(File directory) {
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();

            if (files != null) {
                for (File file : files) {
                    Log.e("##FILE_INFO", "File Name: " + file.getName() + ", File Path: " + file.getAbsolutePath());
                }
            }
        }
    }


    private void logFilesInPodcasts() {
        // Get the "Podcasts" folder
        File podcastsFolder = new File(Environment.getExternalStorageDirectory(), "Download");

        // Log files in the "Podcasts" folder
        logFiles(podcastsFolder);
    }

}

Now, the problem is:

Rather showing the directory of SD card, it is showing the folders under sdk_gphone64_x86_64. Another problem - It is logging till the child directory, but not showing the list of file names under them. So, I need a solution to get all the file names as well under each directory.

I want a solution where I can see all the list of files at any directory of the devices. How can I do that?

1

There are 1 best solutions below

1
Davit Hovhannisyan On

This may help you:

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

public class LogFileListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_file_list);

       
        File rootDirectory = Environment.getExternalStorageDirectory();

      
        logFiles(rootDirectory);

        previously)
        logFilesInDownload();
    }

    private void logFiles(File directory) {
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();

            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                      
                        Log.e("##FILE_INFO", "Directory Name: " + file.getName() + ", Directory Path: " + file.getAbsolutePath());
                       
                        logFiles(file);
                    } else {
                        
                        Log.e("##FILE_INFO", "File Name: " + file.getName() + ", File Path: " + file.getAbsolutePath());
                    }
                }
            }
        }
    }

    private void logFilesInDownload() {
       
        File downloadFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

       
        logFiles(downloadFolder);
    

} }