Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 328
dong221 Avatar asked Apr 02 '11 15:04

dong221


People also ask

Why is my Samsung not recognizing my SD card?

On your Android phone, go to Settings> Storage, find SD card section. If it shows “Mount SD card” or “Unmount SD card” option, perform these operations to fix the problem. This solution has been proved to be able to solve some SD card not recognized problems.

Does Samsung Smart Switch transfer SD card content?

You can transfer the contents from the old phone to a microSD card, and then restore the contents on your new Galaxy phone.


1 Answers

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();
            }
        }
like image 137
nandol Avatar answered Nov 02 '22 23:11

nandol