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?
This may help you:
} }