以编程方式检查SD卡是否可用

nar*_*esh 51 android sd-card

我的应用程序适用于只有SD卡的手机.因此,我想以编程方式检查SD卡是否可用以及如何找到SD卡可用空间.可能吗?

如果是,我该怎么办?

Par*_*ani 126

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable();

if(isSDSupportedDevice && isSDPresent)
{
  // yes SD-card is present
}
else
{
 // Sorry
}
Run Code Online (Sandbox Code Playgroud)

  • 但如果手机有内置存储,则返回true.所以答案不正确 (45认同)
  • 要确定外部存储是否为SDCARD,请结合使用以上内容:Environment.isExternalStorageRemovable() (19认同)
  • 如何检查SD卡空闲内存? (3认同)
  • 在现代Android设备上,外部存储(称为"sdcard")现在也可以是内部存储,只需单独存储即可.所以是的,这是一个很好的答案. (2认同)
  • 对于没有 SD 卡而只有内部存储器的设备,这将返回 true。 (2认同)
  • 在模拟外部存储的情况下,这显然是错误的答案.理想情况下,应使用Environment.isExternalStorageRemovable().您还可以看到Environment.isExternalStorageEmulated().@naresh你不应该接受部分答案. (2认同)

Jem*_*ili 14

接受的答案对我不起作用

Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
Run Code Online (Sandbox Code Playgroud)

如果设备具有内置存储,则返回true; 我的解决方案是检查外部文件目录计数,如果有多个,则设备有sdcard.它工作,我测试了几个设备.

public static boolean hasRealRemovableSdCard(Context context) {
    return ContextCompat.getExternalFilesDirs(context, null).length >= 2;
}
Run Code Online (Sandbox Code Playgroud)


Phi*_*art 13

使用Environment.getExternalStorageState()中的说明"使用外部存储".

要获得外部存储空间,请使用StatFs:

// do this only *after* you have checked external storage state:
File extdir = Environment.getExternalStorageDirectory();
File stats = new StatFs(extdir.getAbsolutePath());
int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize();
Run Code Online (Sandbox Code Playgroud)


小智 13

您可以像这样检查外部可移动 SD 卡是否可用

public static boolean externalMemoryAvailable(Activity context) {
    File[] storages = ContextCompat.getExternalFilesDirs(context, null);
    if (storages.length > 1 && storages[0] != null && storages[1] != null)
        return true;
    else
        return false;

}
Run Code Online (Sandbox Code Playgroud)

这非常有效,因为我已经对其进行了测试。


kao*_*ick 5

我为检查存储状态写了一个小类.也许它对你有用.

import android.os.Environment;

/**
 * Checks the state of the external storage of the device.
 * 
 * @author kaolick
 */
public class StorageHelper
{
// Storage states
private boolean externalStorageAvailable, externalStorageWriteable;

/**
 * Checks the external storage's state and saves it in member attributes.
 */
private void checkStorage()
{
// Get the external storage's state
String state = Environment.getExternalStorageState();

if (state.equals(Environment.MEDIA_MOUNTED))
{
    // Storage is available and writeable
    externalStorageAvailable = externalStorageWriteable = true;
}
else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
    // Storage is only readable
    externalStorageAvailable = true;
    externalStorageWriteable = false;
}
else
{
    // Storage is neither readable nor writeable
    externalStorageAvailable = externalStorageWriteable = false;
}
}

/**
 * Checks the state of the external storage.
 * 
 * @return True if the external storage is available, false otherwise.
 */
public boolean isExternalStorageAvailable()
{
checkStorage();

return externalStorageAvailable;
}

/**
 * Checks the state of the external storage.
 * 
 * @return True if the external storage is writeable, false otherwise.
 */
public boolean isExternalStorageWriteable()
{
checkStorage();

return externalStorageWriteable;
}

/**
 * Checks the state of the external storage.
 * 
 * @return True if the external storage is available and writeable, false
 *         otherwise.
 */    
public boolean isExternalStorageAvailableAndWriteable()
{
checkStorage();

if (!externalStorageAvailable)
{
    return false;
}
else if (!externalStorageWriteable)
{
    return false;
}
else
{
    return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)