gpt4 book ai didi

android - 如何在android中的sdcard中创建文件夹

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:24 24 4
gpt4 key购买 nike

我想在我的 sdcard 中创建文件夹,我使用了下面的代码:

public class Screen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);

operateOnFirstUsage();
}

private void operateOnFirstUsage() {

String state = Environment.getExternalStorageState();
Log.d("Media State", state);

if (Environment.MEDIA_MOUNTED.equals(state)) {
File appDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/");

Log.d("appDirectroyExist", appDirectory.exists() + "");
if (!appDirectory.exists())
Log.d("appDir created: ", appDirectory.mkdir() + "");

File dbDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Database/");

Log.d("dbDirectroyExist", dbDirectory.exists() + "");
if (!dbDirectory.exists())
Log.d("dbDir created: ", dbDirectory.mkdirs() + "");


File themesDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Themes/");
Log.d("themesDirectroyExist", themesDirectory.exists() + "");
if (!themesDirectory.exists())
Log.d("themesDir created: ", themesDirectory.mkdirs() + "");

}
}
}

另外,我设置了sdcard写权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我已经多次运行该应用程序,每次我都得到 LogCat 输出:

01-09 21:38:13.701: D/Media State(15363): mounted
01-09 21:38:13.701: D/appDirectroyExist(15363): false
01-09 21:38:13.701: D/appDir created:(15363): false
01-09 21:38:13.701: D/dbDirectroyExist(15363): false
01-09 21:38:13.701: D/dbDir created:(15363): false
01-09 21:38:13.701: D/themesDirectroyExist(15363): false
01-09 21:38:13.701: D/themesDir created:(15363): false

我读过类似的问题,但没有得到任何有用的信息。我应该怎么做才能让代码运行?我的问题是什么?

最佳答案

已编辑

试试这个:

File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
if(!mydir.exists())
mydir.mkdirs();
else
Log.d("error", "dir. already exists");

并重新检查权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 如何在android中的sdcard中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028247/

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