-3

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.

abeni
  • 3
  • 1

1 Answers1

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..

  • okay but how do i print those "fileOutput"s in a new activity? – abeni May 16 '18 at 08:43
  • that time you need to store fileoutput data into array list and send to other activity. –  May 16 '18 at 08:49
  • do i need to store them in an array list before looping them or after? – abeni May 16 '18 at 08:52
  • 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