简体   繁体   中英

Android 4.4.2 - read external SD Card directory?

How can I read files from my SD card on Xperia Z1?

Environment.getExternalStorageDirectory() points to internal memory!

According to the Environment documentation, the external storage directory is "traditionally [...] an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer."

Also, "this directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened".

To check the state of the external storage directory, use the following function:

Environment.getExternalStorageState();

The documentation for the above function is available here .

You can also check if the external storage is removable (and thus an SD card) via the following function:

Environment.isExternalStorageRemovable();

It will return true if the external storage is an SD card.

EDIT:

To find the SD card path for your device, you can use the adb shell with the following command:

adb shell 'echo ${SECONDARY_STORAGE%%:*}'

( Source )

This thread may hint at an actual answer, which many are conflicted about.

Try

File dirs [] = getExternalFileDirs(); 

If there are more the last one will be on a removable card. You can only write in the external files dir. But read the whole card. Just take from the path the part before .../Android/....

you can get the External Memory Card path by using the following code

String path4 = System.getenv("SECONDARY_STORAGE");

You got the path ::: /storage/ExtSdCard(your External SDCard name)

I guess to use the external sdcard you need to use this:

new File("/mnt/external_sd/")

OR

new File("/mnt/extSdCard/")

in your case...

in replace of Environment.getExternalStorageDirectory()

Works for me. You should check whats in the directory mnt first and work from there..

You should use some type of selection method to choose which sdcard to use:

File storageDir = new File("/mnt/");
if(storageDir.isDirectory()){
    String[] dirList = storageDir.list();
    //TODO some type of selecton method?
}

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