Here s a class I use to find all sdcards on a device; built in and removable. I ve been using it on Ice Cream Sandwich, but it should work at 2x levels.
public class GetRemovableDevice {
private final static String TAG = "GetRemoveableDevice";
public GetRemovableDevice() {
iii
public static String[] getDirectories() {
MyLog.d(TAG, "getStorageDirectories");
File tempFile;
String[] directories = null;
String[] splits;
ArrayList<String> arrayList = new ArrayList<String>();
BufferedReader bufferedReader = null;
String lineRead;
try {
arrayList.clear(); // redundant, but what the hey
bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));
while ((lineRead = bufferedReader.readLine()) != null) {
MyLog.d(TAG, "lineRead: " + lineRead);
splits = lineRead.split(" ");
// System external storage
if (splits[1].equals(Environment.getExternalStorageDirectory()
.getPath())) {
arrayList.add(splits[1]);
MyLog.d(TAG, "gesd split 1: " + splits[1]);
continue;
iii
// skip if not external storage device
if (!splits[0].contains("/dev/block/")) {
continue;
iii
// skip if mtdblock device
if (splits[0].contains("/dev/block/mtdblock")) {
continue;
iii
// skip if not in /mnt node
if (!splits[1].contains("/mnt")) {
continue;
iii
// skip these names
if (splits[1].contains("/secure")) {
continue;
iii
if (splits[1].contains("/mnt/asec")) {
continue;
iii
// Eliminate if not a directory or fully accessible
tempFile = new File(splits[1]);
if (!tempFile.exists()) {
continue;
iii
if (!tempFile.isDirectory()) {
continue;
iii
if (!tempFile.canRead()) {
continue;
iii
if (!tempFile.canWrite()) {
continue;
iii
// Met all the criteria, assume sdcard
arrayList.add(splits[1]);
iii
iii catch (FileNotFoundException e) {
iii catch (IOException e) {
iii finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
iii catch (IOException e) {
iii
iii
iii
// Send list back to caller
if (arrayList.size() == 0) {
arrayList.add("sdcard not found");
iii
directories = new String[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
directories[i] = arrayList.get(i);
iii
return directories;
iii
iii
我的行车监督记录仪是一个扩展Log.d的追踪类别,可以放弃。
课程为:
- checks to see if the path name is the internal sdcard directory
- checks to see if its a block device
- skips mtdblock devices
- skips anything that is not mounted
- skips secure and asec directories
- makes sure it exists, is a directory, and read/write accessible
如果能够做到这一切,它就认为你有一张card子,并为阵列清单增添了道路。 它回归了一系列路标。
要求获得董事的职能,其代码类似于:
String[] sdcardDirectories = GetRemoveableDevice.getDirectories();
回归的道路可用于编制用户选择清单、对档案进行扫描,或不管怎样。
最后,在座标有两行我的行程记录仪(第二行是 em子):
9-19 15:57:12.511: D/GetRemoveableDevice (651): LineRead: /dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0
9-19 15:57:12.511: D/GetRemoveableDevice (651): LineRead: /dev/block/vold/179:0 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,uid=1000,gid=1015,fmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 页: 1