如何在安卓系统中的SD卡创建文件夹

5

我想在我的SD卡中创建文件夹,我使用了以下代码:

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() + "");

        }
    }
}

另外,我已经设置了SD卡的写入权限:

<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 file = new File(Environment.getExternalStorageDirectory(), path); - Triode
仅仅创建一个File对象并不会对SD卡产生任何影响。如果你想要创建一个目录,你需要在那个File对象上调用mkdir()方法。如果你想要创建一个文件,你需要在那个对象上调用createNewFile()方法。 - David Wasser
@RajeshCP 我尝试了,但结果相同。 - Ali Allahyar
是的,我使用了上面Log.d(...)中看到的mkdir()和mkdirs()。 - Ali Allahyar
你能检查外部存储的状态,看它是否可写吗? - Triode
如果我有权限和媒体状态已挂载,这不就足够了吗? - Ali Allahyar
5个回答

13

已编辑

尝试这个:

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" />

你看,我在我的代码中已经做了这个。我必须找到问题的根源。 - Ali Allahyar
只是好奇为什么不使用Environment.getExternalStorageDirectory()会更好?硬编码对所有设备和未来是否可行? - TheLettuceMaster
1
@KickingLettuce:是的,你说得对,我们应该使用Environment.getExternalStorageDirectory(),但是为了测试,我使用/sdcard,在我的所有模拟器和设备上都可以正常工作。感谢您的建议,我已经编辑了我的答案。 - dakshbhatt21
@AliAllahyar:如果您的问题已经解决,可以通过点击下方投票区域的“对勾”标志来将此答案标记为已完成。 - dakshbhatt21
@dakshbhatt21 你可能需要编辑你的代码,以便使用以下代码:File mydir = new File(Environment.getExternalStorageDirectory(), "/mydir/"); - shanwu
@shanwu:两种方法都可以,你发布的代码和我在答案中提供的代码将创建相同的文件。 - dakshbhatt21

1
这是我在sdcard的Pictures文件夹中创建MyDir文件夹的方法:
File mediaStorageDir =  new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyDir");
mediaStorageDir.mkdirs();

我使用了你的方法,新建了一个带有两个参数的文件,并且使用了你的完整代码,但是仍然没有任何效果。 - Ali Allahyar
你试过在不同的手机上吗? - gile

1
尝试这种方式:

File file=new File(Environment.getExternalStorageDirectory() + File.separator +"MyApp/");
file.mkdirs();

我在LogCat中仍然得到false,我已经双重检查了SD卡,没有文件夹被创建。 - Ali Allahyar
我已经尝试过了,它可以正常工作。我有一个应用程序也是同样的工作方式。 - Dharmendra

1
尝试这段代码 :),它很简单。
        String fileName = "CallHistory.pdf";

        // create a File object for the parent directory
        File PDF_Directory = new File("/sdcard/MOBstate/");

        // have the object build the directory structure, if needed.          
        PDF_Directory.mkdirs();

        // create a File object for the output file           
         File outputFile = new File(PDF_Directory, fileName);

        // now attach the OutputStream to the file object, instead of a ring representation
         FileOutputStream fos = new FileOutputStream(outputFile);

0

我不知道为什么应用程序会出现这种情况,但最终我通过在Android清单文件中删除和重置SD卡写入权限来解决了这个问题。感谢大家的帮助,很抱歉浪费了你们的时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接