0

I want to integrate file browser functionality in my android application. I am able to access Internal memory of phone using folowing code snippet:

 Path = new File(Environment.getExternalStorageDirectory().getAbsolutePath());

But I am unable to find the function to read sd cards to show files from those folders. please suggest way to do this.

Yash Jain
  • 55
  • 1
  • 9
  • 1
    Which os version of android you are using. – avinash Jan 16 '17 at 12:21
  • the above code is to read external memory. and context.getFilesDir().getAbsolutePath(); this method is used to read the internal memory – Ankush Bist Jan 16 '17 at 12:21
  • You do not have arbitrary access to [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html) on Android 4.4+. – CommonsWare Jan 16 '17 at 12:30
  • I want my application to work on all android devices > 4.Is there no method there to read files from the SD card for the Android Devices > 4 – Yash Jain Jan 16 '17 at 17:47
  • But if there doesn't exist any way to access sd cards path then how file managers are able to show the files present inside external sd cards.Any help will be appreciated – Yash Jain Jan 23 '17 at 05:39

1 Answers1

-2

To work with external storage you have to edit your Manifest file:

<manifest ...>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

To get list of files from this dir you can use

File[] files = Environment.getExternalStorageDirectory().listFiles();

After adding this to manifest you can read file line-by-line like this:

File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"file.txt");
while ((line = br.readLine()) != null) {
        //whatever you want
    }