我正在使用以下代码来检查Sd卡是否存在并且是可写的。但是当我在模拟器的Sd卡的上下文中使用该代码时,它表明模拟器中不存在Sd卡,但实际上,文件资源管理器显示相应模拟器的Sd卡的内容。这是代码:
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
System.out.println("storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
System.out.println("storage writable is " + writable );
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
此代码显示Sd卡未安装但文件资源管理器显示的是不同的图片。请帮助我。谢谢。
答案 0 :(得分:2)
这项工作将检查已安装且可读或不可读。
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable == true
&& mExternalStorageWriteable == true) {
return true;
} else {
return false;
}
}