如何找到内部SD卡路径?

时间:2012-06-17 09:03:17

标签: android

我是android的初学者。如何找到内部SD卡的路径,然后如何找到他们使用的空间和自由空间。请回复你的意见和答案对我有价值。

5 个答案:

答案 0 :(得分:4)

Environment.getExternalStorageDirectory().getAbsolutePath()提供SD卡路径。

要使外部SD卡可用“免费”空间,请使用以下代码段。

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
double sdAvailSize = (double)stat.getAvailableBlocks()
                   * (double)stat.getBlockSize();
//One binary gigabyte equals 1,073,741,824 bytes.
double gigaAvailable = sdAvailSize / 1073741824;

答案 1 :(得分:2)

  

如何查找内部SD卡路径和外部SD卡路径

你没有。

此时,从Android SDK的角度来看,没有“内部SD卡路径”或“外部SD卡路径”。存在外部存储路径,该路径由Environment上的各种方法返回(例如,getExternalStorageDirectory())。这将是设备制造商为此目的指定的任何存储。

在Android设备上没有可靠的方法来确定任何其他潜在的“SD卡路径”。

答案 2 :(得分:1)

答案 3 :(得分:0)

使用下面的代码,它将返回具有内部和外部存储的设备的内部目录路径和SD卡路径列表。

File dir = new File(Environment.getExternalStorageDirectory().getParent());
            File[] f = dir.listFiles();
            for(File file:f)
        {
            if(file.getName().equals("sdacard0"))//check name of sdcard from  file manager.
            {
                if(file.isDirectory())
                {
                    f = file.listFiles();
                }
            }
        }

答案 4 :(得分:0)

System.getenv(ENV_EXTERNAL_STORAGE)中总是有android.os

源代码中的其他示例:

public static File getExternalStorageDirectory() {
    throwIfUserRequired();
    return sCurrentUser.getExternalDirs()[0];
}

/** {@hide} */
public static File getLegacyExternalStorageDirectory() {
    return new File(System.getenv(ENV_EXTERNAL_STORAGE));
}

/** {@hide} */
public static File getLegacyExternalStorageObbDirectory() {
    return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB);
}

Environment.java

private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE";
private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT";
private static final String ENV_ANDROID_DATA = "ANDROID_DATA";
private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND";
private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE";
private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE";
private static final String ENV_OEM_ROOT = "OEM_ROOT";
private static final String ENV_ODM_ROOT = "ODM_ROOT";
private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT";

/** {@hide} */
public static final String DIR_ANDROID = "Android";
private static final String DIR_DATA = "data";
private static final String DIR_MEDIA = "media";
private static final String DIR_OBB = "obb";
private static final String DIR_FILES = "files";
private static final String DIR_CACHE = "cache";

/** {@hide} */
@Deprecated
public static final String DIRECTORY_ANDROID = DIR_ANDROID;

private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand");
private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage");
private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache");
private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem");
private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm");
private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor");

来源

sources\android-25\android\os\Environment.java