简体   繁体   中英

External Sd-Card Stroage

I am new to android, I tried a lot codes, but can't get the path. but using below code, I got the path of the External Sdcard, but could'nt access the External Sdcard directory.

public static List<StorageInfo> getStorageList() {

    List<StorageInfo> list = new ArrayList<StorageInfo>();
    String def_path = Environment.getExternalStorageDirectory().getPath();
    boolean def_path_internal = !Environment.isExternalStorageRemovable();
    String def_path_state = Environment.getExternalStorageState();
    boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)
            || def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    BufferedReader buf_reader = null;
    try {
        HashSet<String> paths = new HashSet<String>();
        buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
        String line;
        int cur_display_number = 1;
        while ((line = buf_reader.readLine()) != null) {                if (line.contains("vfat") || line.contains("/mnt")) {
            StringTokenizer tokens = new StringTokenizer(line, " ");
            String unused = tokens.nextToken(); //device
            String mount_point = tokens.nextToken(); //mount point
            if (paths.contains(mount_point)) {
                continue;
            }
            unused = tokens.nextToken(); //file system
            List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags
            boolean readonly = flags.contains("ro");

            if (mount_point.equals(def_path)) {
                paths.add(def_path);
                list.add(0, new StorageInfo(def_path, def_path_internal, readonly, -1));
            } else if (line.contains("/dev/block/vold")) {
                if (!line.contains("/mnt/secure")
                        && !line.contains("/mnt/asec")
                        && !line.contains("/mnt/obb")
                        && !line.contains("/dev/mapper")
                        && !line.contains("tmpfs")) {
                    paths.add(mount_point);
                    list.add(new StorageInfo(mount_point, false, readonly, cur_display_number++));
                }
            }
        }
        }

        if (!paths.contains(def_path) && def_path_available) {
            list.add(0, new StorageInfo(def_path, def_path_internal, def_path_readonly, -1));
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (buf_reader != null) {
            try {
                buf_reader.close();
            } catch (IOException ex) {}            }
    }
    return list;
}

How can i access the Sd card directory.

From android 23+ you need to gain user permission to write to the phones external storage. Do this by adding the following to your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If this does not give you access to the SD card then you may need to ask for permission at runtime before trying to access the card. Here is an example:

public void checkPermissions() {
    String[] tmp = {"WRITE_EXTERNAL_STORAGE"};
    for (String s : tmp) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!hasPermissionInManifest(getBaseContext(), s)) {
                if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }
            }
        }
    }
}

You need permissions to access other sensors in the phone and camera which can be done in a similar way. You can get the path to the external storage like this:

Environment.getExternalStorageDirectory().toString();

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