How do i get the list of all saved text files in (storage/emulated/0/Notes, in this case "Notes" is my folder where all my saved text files are located) and make them available in a new activity(generate a textView for every single file to be printed). i want all this to happen when for instance an "Open" button is clicked. THANKS in advance.
list all saved text files on a storage folder to my activity when a button is clicked android studio
Asked
Active
Viewed 259 times
-3
-
File.listFiles() ? – greenapps May 16 '18 at 08:18
-
It should be `/storage/emulated/0/Notes`. Know your paths! – greenapps May 16 '18 at 08:19
-
Please refer the below link https://stackoverflow.com/questions/9530921/list-all-the-files-from-all-the-folder-in-a-single-list – Absar Alam May 16 '18 at 08:21
1 Answers
0
Try this code..
// take below variable as global level of class
public List<String> filesNames=new ArrayList<>();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// gets the files in the directory it fatch internal storage
File fileDirectory = new File(Environment.getDataDirectory()+"/YourDirectory/");
// below code for fatch sd card files on define folder;
File fileDirectorySD = new File(Environment.getExternalStorageDirectory()+"/YourDirectory/");
// lists all the files into an array
File[] dirFiles = fileDirectory.listFiles();
if (dirFiles.length != 0) {
// loops through the array of files, outputing the name to console
for (int ii = 0; ii < dirFiles.length; ii++) {
String fileOutput = dirFiles[ii].toString();
filesNames.add(fileOutput);
System.out.println(fileOutput);
}
}
}
});
and if you are run app in marshmallow above device then give read permission and also handling permission model..
-
-
that time you need to store fileoutput data into array list and send to other activity. – May 16 '18 at 08:49
-
-
Thanks , but the "fileNames" are not clickable. If you could show me how i can make them clickable? – abeni May 16 '18 at 09:03
-
fileNames are not clickable it simple text .you show fileNames into listview or recyclerview. – May 16 '18 at 09:06