1

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

Environment.getExternalStorageDirectory() points to internal memory!

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91

4 Answers4

0

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.

Community
  • 1
  • 1
BVB
  • 5,380
  • 8
  • 41
  • 62
  • But.... File Explorer can detect it even when it is connected to USB, so how does it know?? – Flash Thunder Jun 18 '14 at 16:48
  • This does not answer the question. The problem is the external storage @FlashThunder trying to access is an enulated internal storage, but that returned from `getExternalStorageDirectory()`. The *real* external storage cannot be accessed this way. – WonderCsabo Jun 18 '14 at 16:52
  • That's true, @WonderCsabo. It seems that it is up to manufacturers to specify what is returned when the external storage is requested. – BVB Jun 18 '14 at 17:02
0

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/....

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

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)

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
-1

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?
}
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44