• Android: как писать файлы в removable external storage?

    @alexkipar
    public class StorageUtils {

    public static class StorageInfo {

    public final String path;
    public final boolean readonly;
    public final boolean removable;
    public final int number;

    StorageInfo(String path, boolean readonly, boolean removable, int number) {
    this.path = path;
    this.readonly = readonly;
    this.removable = removable;
    this.number = number;
    }

    public String getDisplayName() {
    StringBuilder res = new StringBuilder();
    if (!removable) {
    res.append("Internal SD card");
    } else if (number > 1) {
    res.append("SD card " + number);
    } else {
    res.append("SD card");
    }
    if (readonly) {
    res.append(" (Read only)");
    }
    return res.toString();
    }
    }

    public static List getStorageList() {

    List list = new ArrayList();
    String defaultPath = Environment.getExternalStorageDirectory().getPath();
    boolean defaultPathRemovable = Environment.isExternalStorageRemovable();
    String defaultPathState = Environment.getExternalStorageState();
    boolean defaultPathAvailable = defaultPathState.equals(Environment.MEDIA_MOUNTED) || defaultPathState.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    boolean defaultPathReadonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);

    HashSet paths = new HashSet();
    int curRemovableNumber = 1;

    if (defaultPathAvailable) {
    paths.add(defaultPath);
    list.add(0, new StorageInfo(defaultPath, defaultPathReadonly, defaultPathRemovable, defaultPathRemovable ? curRemovableNumber++ : -1));
    }

    BufferedReader reader = null;
    try {
    reader = new BufferedReader(new FileReader("/proc/mounts"));
    String line;
    while ((line = 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 flags = Arrays.asList(tokens.nextToken().split(",")); //flags
    boolean readonly = flags.contains("ro");

    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, readonly, true, curRemovableNumber++));
    }
    }
    }
    }

    } catch (FileNotFoundException ex) {
    ex.printStackTrace();
    } catch (IOException ex) {
    ex.printStackTrace();
    } finally {
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException ex) {}
    }
    }
    return list;
    }
    }
    Ответ написан
    Комментировать