English 中文(简体)
如何进入可移动的安乐器储存?
原标题:How to access removable storage on Android devices?

我正在寻找一种途径,在各种安乐器(Samsung、摩托拉、LG、Sony、HTC)上发现和获取可移动的卡片。

我还需要与2.2条相容,即:环境。

摩托拉有自己的图书馆,对三星一来说,可以发现有<代码>/外部_sd/。

我没有其他名字。 例如,我看到了一个<代码>/_ExternalSD/ on some LG s,但即使在SD被删除时,该名录仍然保留。

A bonus question: will the ACTION_MEDIA_MOUNTED intent be broadcast for any of them

这样做会非常有益。

问题回答

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的追踪类别,可以放弃。

课程为:

  1. checks to see if the path name is the internal sdcard directory
  2. checks to see if its a block device
  3. skips mtdblock devices
  4. skips anything that is not mounted
  5. skips secure and asec directories
  6. 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

根据Howards等人的类别,我做了一些修改,以在银河S3上开展工作。

  1. Environment.getExternalStorageDirectory() returns internal storage on the S3.
  2. Removable storage is not necessarily mounted under /mnt
  3. Removable media must have vfat filesystem

——

public static String getDirectory() {
        Log.d(TAG, "getStorageDirectories");
        File tempFile;
        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) {
                Log.d(TAG, "lineRead: " + lineRead);
                splits = lineRead.split(" ");

                // skip if not external storage device
                if (!splits[0].contains("/dev/block/")) {
                    continue;
                }

                // skip if mtdblock device
                if (splits[0].contains("/dev/block/mtdblock")) {
                    continue;
                }

                // skip if not in vfat node
                if (!splits[2].contains("vfat")) {
                    continue;
                }

                // skip these names
                if (splits[1].contains("/secure")) {
                    continue;
                }

                if (splits[1].contains("/mnt/asec")) {
                    continue;
                }

                // Eliminate if not a directory or fully accessible
                tempFile = new File(splits[1]);
                if (!tempFile.exists()) {
                    continue;
                }
                if (!tempFile.isDirectory()) {
                    continue;
                }
                if (!tempFile.canRead()) {
                    continue;
                }
                if (!tempFile.canWrite()) {
                    continue;
                }

                // Met all the criteria, assume sdcard
                return splits[1];
            }

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                }
            }
        }

        return null;
    }

根据Howards等人的类别一,进一步修改了这一类别,指出我可以发现的在几部电话和桌子上的所有外部可移动的储存都是用电压、数量不断增加的 da。

            // skip if not external storage device
            if (!splits[0].contains("vold")) {
                continue;
            }

            if (splits[1].contains("/mnt/asec")) {
                continue;
            }

            // Eliminate if not a directory or fully accessible
            tempFile = new File(splits[1]);
            if (!tempFile.exists()) {
                continue;
            }
            if (!tempFile.isDirectory()) {
                continue;
            }
            if (!tempFile.canRead()) {
                continue;
            }
            if (!tempFile.canWrite()) {
                continue;
            }

            // Met all the criteria, assume sdcard
            arrayList.add(splits[1]);

This is the method I created and am using. This has worked on Samsung Galaxy S4, Samsung Galaxy Note 3 and Sony Xperia Z2.

private static String[] getRemovableStoragePaths() {
    String[] directories;
    String[] splits;
    ArrayList<String> pathList = new ArrayList<String>();
    BufferedReader bufferedReader = null;
    String lineRead;

    try {
        bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

        while ((lineRead = bufferedReader.readLine()) != null) {
            Log.d(TAG, "lineRead: " + lineRead);
            splits = lineRead.split(" ");
            Log.d(TAG, "Testing path: " + splits[1]);

            if (!splits[1].contains("/storage")) {
                continue;
            }

            if (splits[1].contains("/emulated")) {
                // emulated indicates an internal storage location, so skip it.
                continue;
            }

            // Eliminate if not a directory or fully accessible
            Log.d(TAG, "Path found: " + splits[1]);

            // Met all the criteria, assume sdcard
            pathList.add(splits[1]);
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
            }
        }
    }

    // Send list back to caller

    if (pathList.size() == 0) {
        pathList.add("sdcard not found");
    } else {
        Log.d(TAG, "Found potential removable storage locations: " + pathList);
    }
    directories = new String[pathList.size()];
    for (int i = 0; i < pathList.size(); i++) {
        directories[i] = pathList.get(i);
    }
    return directories;
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...