-1

How to read folders & files of internal(storage) & external(sd card) in android?

I am using bellow code, but its gets only folders from storage device, I want files & folders from sd card also, Please suggest how to get or any code. I am searching, but lot of waste of my time.

String extStore = Environment.getExternalStorageDirectory().getAbsolutePath();
Nitin Karale
  • 789
  • 3
  • 12
  • 34

2 Answers2

0

This is the function to get a file from external storage:-

private File getFileFromSD(String fileName)
{
   File sdcard = Environment.getExternalStorageDirectory();
   return new File(sdcard,fileName);
}

Function implementation:-

/* For example, you have a text file(text.txt) and you want to read the content of it*/   
StringBuilder fileData= new StringBuilder(); /*At the end of the execution all the text stored on the file will be pasted on the StringBuiler() that is fileData.*/ 
try {
    BufferedReader buffer = new BufferedReader(new FileReader(getFileFromSD("text.txt")));
    String data;   
    while ((data= buffer.readLine()) != null) {
        fileData.append(data);
        fileData.append('\n');
    }
    buffer.close();
}
catch (IOException e) {
   Log.e(TAG,e.getMessage());
}
Shashank Mishra
  • 542
  • 4
  • 12
0

try this

  private void FetchData() {

        ArrayList<String> filenames = new ArrayList<String>();

        String path = Environment.getExternalStorageDirectory();

        File directory = new File(path);

        File[] files = directory.listFiles();

        for (int i = 0; i < files.length; i++)
        {
            String file_name = files[i].getName();

            filenames.add(file_name);
            filecount+=1;
            imageList.add(path+file_name);
        }
//        return filenames;
    }
Mitesh Makwana
  • 99
  • 1
  • 10