简体   繁体   中英

Finding SD-Card Path in Android 4.4 Kitkat

In one of my application i'm accessing some files in the sd card. I'm using the below function to determine the mounted sd-card path.

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

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

    return path;

This works for all the devices prior to Kitkat. But in Kitkat version i'm getting FileNotFoundException on line 1,

File file = new File("/system/etc/vold.fstab");

I found so many articles about updated sd card permission in Android kitkat version.But still a bit confused about that... Please can anyone help me to sort it out??? Thanks in advance...

vold.fstab is no longer used in 4.3. Instead, fstab. is now present.

Refer to http://source.android.com/devices/tech/storage/ although it is not very clear

You must call to :

File sdCardPath = Environment.getExternalStorageDirectory();

to know the path of SD card. This function call invoke system operative function wich determine the path of SD card. And to know the state of external storage you must use

Environment.getExternalStorageState() 

function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM