我的应用仅适用于有SD卡的移动设备。因此,我想通过编程来检查SD卡是否可用以及如何查找SD卡的剩余空间。这是否可以实现?
如果可以,我该怎么做呢?
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
}
被接受的答案对我没用。
Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
如果设备有内置存储,则返回true;我的解决方案是检查外部文件目录的数量,如果大于1,则设备具有SD卡。 它有效,并且我已经对多个设备进行了测试。
public static boolean hasRealRemovableSdCard(Context context) {
return ContextCompat.getExternalFilesDirs(context, null).length >= 2;
}
5.1
上测试过了,运行良好。非常感谢。 - M DEV您可以按照以下方式检查外置可移动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;
}
我已经测试过,这个功能完美运作。
按照"使用外部存储"中的描述,使用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();
我写了一个小类来检查存储状态,也许对你有用。
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;
}
}
}
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder";
}
else
{
path = theAct.getFilesDir() + "/GrammarFolder";
}
void updateExternalStorageState() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
handleExternalStorageState(mExternalStorageAvailable,
mExternalStorageWriteable);
}
这个简单的方法对我很有效,在所有类型的设备上测试过。
public boolean externalMemoryAvailable() {
if (Environment.isExternalStorageRemovable()) {
//device support sd card. We need to check sd card availability.
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED) || state.equals(
Environment.MEDIA_MOUNTED_READ_ONLY);
} else {
//device not support sd card.
return false;
}
}
Kotlin
fun Context.externalMemoryAvailable(): Boolean {
val storages = ContextCompat.getExternalFilesDirs(this, null)
return storages.size > 1 && storages[0] != null && storages[1] != null
}
public static boolean hasSdCard(Context context) {
File[] dirs = context.getExternalFilesDirs("");
if(dirs.length >= 2 && dirs[1]!=null){
if(Environment.isExternalStorageRemovable(dirs[1])){ // Extra Check
return true;
}
}
return false;
}