Home » Android » android – How could i get the correct external storage on Samsung and all other devices?

android – How could i get the correct external storage on Samsung and all other devices?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

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/";
  }

Answer:

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();
            }
        }

Answer:

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/

Answer:

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.

Answer:

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