gpt4 book ai didi

android - 如何高效检测SD卡是否存在且可写?

转载 作者:行者123 更新时间:2023-11-29 21:57:12 27 4
gpt4 key购买 nike

我正在使用以下代码来检查 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 卡,但文件资源管理器显示不同的图片。请帮助我。在此先感谢。

最佳答案

这项工作将检查挂载和是否可读。

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;
}
}

关于android - 如何高效检测SD卡是否存在且可写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12949649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com