外部Sd-Card Stroage

时间:2016-11-12 07:17:10

标签: android memory storage

我是android的新手,我尝试了很多代码,但无法获得路径。 但是使用下面的代码,我得到了外部Sdcard的路径,但是无法访问外部Sdcard目录。

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

如何访问Sd卡目录。

1 个答案:

答案 0 :(得分:0)

从android 23+开始,你需要获得用户写入手机外部存储的权限。通过在清单中添加以下内容来执行此操作:

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

如果这不能让您访问SD卡,那么在尝试访问该卡之前,您可能需要在运行时请求许可。这是一个例子:

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

您需要具有访问手机和相机中其他传感器的权限,这可以通过类似方式完成。您可以像这样获取外部存储的路径:

Environment.getExternalStorageDirectory().toString();