2

我想获得设备内部和外部空间的数量,在浏览了 StackOverflow 上的几篇文章后,我发现这很容易。我可以使用以下方法获取内部空间量:

StatFs sfsInternal = new StatFs(Environment.getRootDirectory().getAbsolutePath());
return Long.valueOf(sfsInternal.getBlockCount()) * Long.valueOf(sfsInternal.getBlockSize());

...我可以使用这个来获得外部空间的数量:

StatFs sfsExternal = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
return Long.valueOf(sfsExternal.getBlockCount()) * Long.valueOf(sfsExternal.getBlockSize());

当我读到“内部”存储时,我认为它是设备上不可移动的板载存储,而“外部”是可移动闪存卡存储,但情况并非完全如此。

我发现三星设备 ee Galaxy Note 2 将很大一部分内部存储显示为外部存储。这是一个讨论同一件事的答案。https://stackoverflow.com/a/12087556/304151

在考虑三星 Galaxy 设备的边缘情况时,如何获得内部存储空间(板载和不可移动)和外部存储空间(闪存和可移动)的数量。我尚未在 StackOverflow 上找到为这种情况提供完整工作解决方案的答案。我的代码适用于 API 级别 17。

谢谢。

4

4 回答 4

0

这是在不同设备上获取可用空间的代码,我在 samsung GALAXY Tab7 2.2 Froyo 和 Nexus 7 4.2.2 Jelly Beans 上测试了该代码

    // calculate frespace on external storage
    public static int getExternalStorageFreeSpace(String storagePath)
        {
            try
                {
                    File file = new File(storagePath);
                    StatFs stat = new StatFs(file.getPath());
                    double sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
                    int valueinmb = (int) (sdAvailSize / 1024) / 1024;
                    return valueinmb;
                }
            catch (Exception e)
                {
                    System.out.println("Message//////" + e.getMessage() + "Cause555555555555" + e.getCause());
                }
            return 0;
        }

为了区分内部和外部存储,我使用了这个类和一些逻辑

public class GetRemoveableDevices
{
    private final static String TAG = "GetRemoveableDevice";

    public GetRemoveableDevices()
        {
        }

    public static String[] getDirectories()
        {
            Log.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)
                        {
                            Log.d(TAG, "lineRead: " + lineRead);
                            splits = lineRead.split(" ");

                            // System external storage
                            if (splits[1].equals(Environment.getExternalStorageDirectory().getPath()))
                                {
                                    arrayList.add(splits[1]);
                                    Log.d(TAG, "gesd split 1: " + splits[1]);
                                    continue;
                                }

                            // 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 /mnt node

                            if (!splits[1].contains("/mnt"))
                                {
                                    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
                            arrayList.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 (arrayList.size() == 0)
                {
                    arrayList.add("sdcard not found");
                }
            directories = new String[arrayList.size()];
            for (int i = 0; i < arrayList.size(); i++)
                {
                    directories[i] = arrayList.get(i);
                }
            return directories;
        }

}

现在我向你展示我的逻辑

String[] dirs = GetRemoveableDevices.getDirectories();
            ArrayList<String> directories=new ArrayList<String>();
            for(String directory:dirs)
                {
                    if(!directory.contains("."))
                    directories.add(directory);
                }
            String externalStorage = "";
            String internalStorage = "";
            if (directories.size()>= 2)
                {
                    internalStorage = directories.get(0).toString();
                    externalStorage = directories.get(1).toString();
                }
            else if (directories.size() < 2)
                {
                    internalStorage = directories.get(0).toString();
                    externalStorage = null;
                }

希望它会有所帮助

于 2013-07-01T11:13:02.560 回答
0

“内部存储”用于私人持有的数据。它被称为内部,因为它与应用程序本身相关。它用于对应用程序的数据进行沙盒处理并保持其私密性。

Environment.getRootDirectory() 获取手机的系统文件夹。这不是内部存储,而是外部存储。

外部存储用于应用程序外部的公共共享数据。

由于手机之间的挂载命名约定差异很大,因此很难区分 SD 卡和普通的板载目录。但一般情况下,sd卡挂载到目录/sdcard/。

于 2013-07-01T11:14:10.227 回答
0

我在这篇博文中找到了一些执行此操作的代码。

package com.sapien.music.importer.util;

import java.io.File;

@SuppressLint("NewApi")
public class StorageOptions {
public static String[] labels;
public static String[] paths;
public static int count = 0;

private static Context sContext;
private static ArrayList<String> sVold = new ArrayList<String>();

public static void determineStorageOptions(Context context) {
    sContext = context.getApplicationContext();

    readVoldFile();

    testAndCleanList();

    setProperties();
}

private static void readVoldFile() {
    /*
     * Scan the /system/etc/vold.fstab file and look for lines like this:
     * dev_mount sdcard /mnt/sdcard 1
     * /devices/platform/s3c-sdhci.0/mmc_host/mmc0
     * 
     * When one is found, split it into its elements and then pull out the
     * path to the that mount point and add it to the arraylist
     * 
     * some devices are missing the vold file entirely so we add a path here
     * to make sure the list always includes the path to the first sdcard,
     * whether real or emulated.
     */

    sVold.add("/mnt/sdcard");

    try {
        Scanner scanner = new Scanner(new File("/system/etc/vold.fstab"));
        while (scanner.hasNext()) {
            String line = scanner.nextLine();
            if (line.startsWith("dev_mount")) {
                String[] lineElements = line.split(" ");
                String element = lineElements[2];

                if (element.contains(":"))
                    element = element.substring(0, element.indexOf(":"));

                if (element.contains("usb"))
                    continue;

                // don't add the default vold path
                // it's already in the list.
                if (!sVold.contains(element))
                    sVold.add(element);
            }
        }
    } catch (Exception e) {
        // swallow - don't care
        e.printStackTrace();
    }
}

private static void testAndCleanList() {
    /*
     * Now that we have a cleaned list of mount paths, test each one to make
     * sure it's a valid and available path. If it is not, remove it from
     * the list.
     */

    for (int i = 0; i < sVold.size(); i++) {
        String voldPath = sVold.get(i);
        File path = new File(voldPath);
        if (!path.exists() || !path.isDirectory() || !path.canWrite())
            sVold.remove(i--);
    }
}

private static void setProperties() {
    /*
     * At this point all the paths in the list should be valid. Build the
     * public properties.
     */

    ArrayList<String> labelList = new ArrayList<String>();

    int j = 0;
    if (sVold.size() > 0) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD)
            labelList.add("Auto");
        else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            if (Environment.isExternalStorageRemovable()) {
                labelList.add(sContext
                        .getString(R.string.text_external_storage) + " 1");
                j = 1;
            } else
                labelList.add(sContext
                        .getString(R.string.text_internal_storage));
        } else {
            if (!Environment.isExternalStorageRemovable()
                    || Environment.isExternalStorageEmulated())
                labelList.add(sContext
                        .getString(R.string.text_internal_storage));
            else {
                labelList.add(sContext
                        .getString(R.string.text_external_storage) + " 1");
                j = 1;
            }
        }

        if (sVold.size() > 1) {
            for (int i = 1; i < sVold.size(); i++) {
                labelList.add(sContext
                        .getString(R.string.text_external_storage)
                        + " " + (i + j));
            }
        }
    }

    labels = new String[labelList.size()];
    labelList.toArray(labels);

    paths = new String[sVold.size()];
    sVold.toArray(paths);

    count = Math.min(labels.length, paths.length);

    /*
     * don't need these anymore, clear the lists to reduce memory use and to
     * prepare them for the next time they're needed.
     */
    sVold.clear();
}
于 2013-07-11T10:31:30.307 回答
0

试试这个:

 private boolean is_sdCardSaveToUse(){

    /**default disk cache size in bytes*/
    final int DEFAULT_DISK_CACHE_SIZE = 1024 * 1024 * 10; //10 MB

    /**get sdCard state*/
    String sdCardState = Environment.getExternalStorageState();

    /**check if the sdCard is mounted*/
    /**check if we can write to sdCard*/if (Environment.MEDIA_MOUNTED.equals(sdCardState)) {
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(sdCardState)) {
            Log.d("sdCard", "mounted readOnly");
        } else {
            Log.d("sdCard", "mounted readWrite");

            /**get free usable space in bytes */
            long freeUsableSpace = Environment.getExternalStorageDirectory().getUsableSpace();
            int temp = Math.round(((float) freeUsableSpace / 1024) / 1024); //convert from bytes to MB.
            Log.d("usableSpace= ", Integer.toString(temp) + " MB");

            if (freeUsableSpace > DEFAULT_DISK_CACHE_SIZE){
                return  true;
            } else {
                Log.d("sdCard","not enough space");
                return  false;
            }

        }

    } else{
        Log.d("sdCard","not mounted");
        return false;
    }

   return false;
}
于 2014-05-24T01:38:24.730 回答