2

When using Eclipse file explorer to navigate my android directories, I saw mnt/sdcard and mnt/sdcard2, see below image:

When callingEnvironment.getExternalStorageDirectory() it returns mnt/sdcard, so I think the mnt/sdcad is the external storage , and mnt/sdcard2 is my actual SD card, is that true? And how can I use code to access files under mnt/sdcard2 ?

enter image description here

P.S.

It seems that I can access the external sd card directly:

    File extStorageDir = Environment.getExternalStorageDirectory();
    String parent = extStorageDir.getParent();
    File extSdCardDir = new File(parent+"/sdcard2");

    File file = new File(extSdCardDir, "DemoFile.jpg");

But I wonder the extra sd card will change name in other cases.

Zhi Wang
  • 1,138
  • 4
  • 20
  • 34
  • i had the same problem in samsung galaxy s3 one is sdcard0 which is the external storage on phone and extSdCard which is the external sd card. this `Environment.getExternalStorageDirectory()` returns the one external storage on your phone. http://developer.android.com/reference/android/os/Environment.html – Raghunandan Sep 30 '13 at 06:03
  • @Raghunandan so no way to access the external sd card? – Zhi Wang Sep 30 '13 at 06:07
  • check this http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory() for more info – Raghunandan Sep 30 '13 at 06:08
  • Quoting form docs In devices with multiple "external" storage directories (such as both secure app storage and mountable shared storage), this directory represents the "primary" external storage that the user will interact with. here's a similar answer by commons ware http://stackoverflow.com/questions/17215466/environment-getexternalstoragedirectory-getabsolutepath-gives-a-different-pa – Raghunandan Sep 30 '13 at 06:11

1 Answers1

1

You are correct, getExternalStorage will return your built-in external storage. Unfortunately, as of Jelly Bean applications are no longer able to utilize the SD card if the device also has built-in storage as well as an SD card. You can try working around it through shell commands or hardcoding paths, but without root there is no reliable way to access it anymore.

This was just recently added the Android CTS, which all OEMs must comply with in order to use the Play store.

Compatibility Program Overview | Android Developers

Section 9.5 (pg. 34) of Android 4.3 Compatibility Definition

Device implementations that include multiple external storage paths MUST NOT allow Android applications to write to the secondary external storage.

Storage Options | Android Developers

It's possible that a device using a partition of the internal storage for the external storage may also offer an SD card slot. In this case, the SD card is not part of the external storage and your app cannot access it (the extra storage is intended only for user-provided media that the system scans).

Android 4.2 APIs | Android Developers

Saving data in a multi-user environment

Whenever your app saves user preferences, creates a database, or writes a file to the user’s internal or external storage space, that data is accessible only while running as that user.

To be certain that your app behaves properly in a multi-user environment, do not refer to your internal app directory or external storage location using hard-coded paths and instead always use the appropriate APIs:

For access to internal storage, use getFilesDir(), getCacheDir(), or openFileOutput().
For access to external storage, use getExternalFilesDir() or getExternalStoragePublicDirectory(). 

No matter which of these APIs you use to save data for a given user, the data will not be accessible while running as a different user. From your app’s point of view, each user is running on a completely separate device.

Jon
  • 1,398
  • 9
  • 14
  • Is that true? I installed one file explorer app on my device and it correctly listed the files under the external sd card. – Zhi Wang Sep 30 '13 at 06:08
  • I'm afraid so. Not a very big fan of it personally, but I'm sure Google has their reasons for it. – Jon Sep 30 '13 at 06:16
  • It seems that I can access the external sd card directly. – Zhi Wang Sep 30 '13 at 06:32
  • That isn't recommended as they have already made clear their intentions to break apps ability to do so. That also isn't very reliable across different OEMs since some htc devices use `/sdext` as well as `/sdcard`. – Jon Sep 30 '13 at 06:46