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)
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)
这非常有效,因为我已经对其进行了测试。
我为检查存储状态写了一个小类.也许它对你有用.
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)
归档时间: |
|
查看次数: |
52454 次 |
最近记录: |