我想在 Sdcard 中创建目录,并且我确实遵循:
- 我补充道:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单中。
- 我通过以下方式获取 root_path:
public static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + "/Hello_World/";
它返回/storage/emulated/0/Hello_World
(调试时获取)。
接下来,我运行这段代码:
File file = new File(Constants.ROOT_PATH);
int i = 0;
while (!file.isDirectory() && !file.mkdirs()) {
file.mkdirs();
Log.e("mkdirs", "" + i++);
}
我也尝试过mkdirs()
and mkdir()
但它在 logcat 中显示无限循环(Log.e("mkdirs", "" + i++);
)。有时有效,但有时无效。
谢谢你的帮助!
Update
:我在某些设备上尝试了我的代码:Nexus 4、Nexus 7、Vega Iron、Genymotion、LG G Pro,然后只有 Vega Iron 按预期工作。 ??!!!?!?
尝试这样它将在中创建一个文件夹sd card
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/hello_world");
myDir.mkdirs();
如果您想检查该文件是否存在,请使用此代码
File file = new File (myDir, file_name);
if (file.exists ())
// file exist
else
// file not exist
参考一下这个答案Android 将文件保存到外部存储 https://stackoverflow.com/a/7887114/964741
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)