简体   繁体   中英

How can one read files from an SD card in Android 7.0?

For a previous telephone (Android 4.0) I wrote an app which uses data files which I copy to the SD card from a PC, using a USB cable. The app uses Environment.getExternalStorageDirectory() to retrieve the files, as advised by many sites.

On a telephone with Android 7.0, this app no longer works. Environment.getExternalStorageDirectory() returns "storage/emulated/0/" which maps to internal storage. I tried getExternalStoragePublicDirectory, but this references the same location.

So I tried a hard-coded "/sdcard". This also does not reference the SD card. On writing a test file to that location from an app, I found the file using the File Commander app in internal storage, but the file could not be found either in internal storage or on the SD card using Windows Explorer on the PC, so it is not clear to me where it really is. In my web searching I have seen references to /sdcard0 and /sdcard1 but testing these revealed that neither exists on my telephone.

My main question is therefore: how do I get hold of the root directory of the SD card in Android 7.0 and later versions, given that getExternalStorageDirectory() does not provide this?

And a subsidiary question: what is the point of a getExternalStorageDirectory() function which does not get the external storage directory?

You could look at the second item returned by getExternalFilesDirs() to find your app specific directory on the SD card.

But if you want to update your app to modern standards then have a look at Storage Access Framework using Intent.ACTION_OPEN_DOCUMENT_TREE and or StorageVolumes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM