Skip to content Skip to sidebar Skip to footer

How Could I Get The Correct External Storage On Samsung And All Other Devices?

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

Solution 1:

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

Filefile=newFile("/system/etc/vold.fstab");
        FileReaderfr=null;
        BufferedReaderbr=null;

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

        try {
            if (fr != null) {
                br = newBufferedReader(fr);
                Strings= br.readLine();
                while (s != null) {
                    if (s.startsWith("dev_mount")) {
                        String[] tokens = s.split("\\s");
                        path = tokens[2]; //mount_pointif (!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();
            }
        }

Solution 2:

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

Solution 3:

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

final File[] appsDir=ContextCompat.getExternalFilesDirs(getActivity(),null);
      final ArrayList<File> extRootPaths=newArrayList<>();
      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/

Solution 4:

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.

Solution 5:

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

Post a Comment for "How Could I Get The Correct External Storage On Samsung And All Other Devices?"