A
A
Alexander Vasyuchenko2014-09-29 10:53:50
Android
Alexander Vasyuchenko, 2014-09-29 10:53:50

Android: how to write files to removable external storage?

Good afternoon!
My device has two external storage mounted: internal memory (as primary external storage) and a removable memory card (as secondary external storage).
How do I programmatically write files to a removable card (secondary external storage) if I have axis version 4.0.3 (API 15)?
ps Environment.getExternalStorageDirectory() returns primary external storage (in my case it is internal emulated storage)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexkipar, 2014-10-01
@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.
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++));
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {}
}
}
return list;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question