xRobot xRobot - 1 month ago 22
Android Question

Get list of files on external SD

I need a way to get all txt files from the SD cards.

I wrote this code:

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
myTxt = findTxt(Environment.getExternalStorageDirectory());
}


But it returns me all txt files of the internal memory of the smartphone.

How is this possible ?

Answer

You are conflating external storage and removable storage. You do not have filesystem-level access to all of removable storage on Android 4.4+.

If you are willing to find all text files anywhere, query the MediaStore, which will index removable storage. Just bear in mind that you will then need to get at the contents of the text files via a Uri that you get from the MediaStore, using things like ContentProvider and openInputStream().