8

The official method Environment.getExternalStorageDirectory() seems to be not working on Samsung devices...

It return "/sdcard/" but not the real path "/sdcard/external_sd/", you could refer to the following post

Android SD Card Characteristics on Samsung Galaxy

Is there another methods to detect the external real storage path and suitable for all devices ?? or just teach me how to avoid this issue ??

Thanks.

Community
  • 1
  • 1
dong221
  • 3,390
  • 6
  • 29
  • 31
  • 1
    such kind of non-standard implementation would ultimately kill the platform! – xandy Apr 02 '11 at 15:43
  • In my case, I have to add toLowerCase() because Galaxy S2 returns "samsung", not "Samsung" – Tanin Nov 19 '11 at 08:29
  • See my answer here: [Code to get all storages on Android][1] [1]: http://stackoverflow.com/questions/6156649/is-there-a-documented-way-in-android-2-x-to-inspect-multiple-sd-cards-for-cont/18870968#18870968 – Dmitriy Lozenko Sep 18 '13 at 11:50

5 Answers5

9

I hope the code below is helpful. That code finds the path of removable external storage (i.e. SD card).

        File file = new File("/system/etc/vold.fstab");
        FileReader fr = null;
        BufferedReader br = null;

        try {
            fr = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 

        try {
            if (fr != null) {
                br = new BufferedReader(fr);
                String s = br.readLine();
                while (s != null) {
                    if (s.startsWith("dev_mount")) {
                        String[] tokens = s.split("\\s");
                        path = tokens[2]; //mount_point
                        if (!Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)) {
                            break;
                        }
                    }
                    s = br.readLine();
                }
            }            
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }            
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
nandol
  • 91
  • 1
  • 3
7

Could you not try something like:

  String url = Environment.getExternalStorageDirectory();
  if(android.os.Build.DEVICE.contains("Samsung") || android.os.Build.MANUFACTURER.contains("Samsung")){
            url = url + "/external_sd/";
  }
Blundell
  • 75,855
  • 30
  • 208
  • 233
4

In order to get all external storage paths, you can use ContextCompat.getExternalFilesDirs() :

      final File[] appsDir=ContextCompat.getExternalFilesDirs(getActivity(),null);
      final ArrayList<File> extRootPaths=new ArrayList<>();
      for(final File file : appsDir)
        extRootPaths.add(file.getParentFile().getParentFile().getParentFile().getParentFile());

The first one is the primary external storage, and the rest are supposed to be real SD-cards paths.

The reason for the multiple ".getParentFile()" is to go up another folder, since the original path is

.../Android/data/YOUR_APP_PACKAGE_NAME/files/
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

Is there another methods to detect the external real storage path and suitable for all devices ?

No. Android supports one "external storage real path" via the SDK. In the case of these Samsung devices, there is more than one "external storage". There is no SDK-supplied way to get at any other storage paths.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

iF you are making an application particularly for Samsung galaxy then the solution provided by Blundell is the best. However, you must ensure that all samsung galaxy tablets/handheld devices share a common directory structure. For example, in certain samsung devices (with lower storage specs), you may not be able to find /mnt/sdcard/external_sd; rather you would have to consider /data as internal storage and /mnt/sdcard as external storage. Moreover, it is not guaranteed that all samsung devices will have the same common directory structure that you are hard-coding. So, just ensure that you check directory structures of different samsung devices. If you are making a universal application (for all Android devices), then I am afraid that this approach won't work unless Android provides an API or the manufacturer comes with its own API like Motorola External Storage API that you can find here http://developer.motorola.com/docstools/library/motorola-external-storage-api/. Good luck

Farhan
  • 3,206
  • 14
  • 49
  • 62