如何在android中创建应用程序文件夹

时间:2013-03-14 06:33:20

标签: android

我正在onCreate()方法的第一个活动上创建应用程序文件夹但是文件夹没有创建。这是代码

 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        File fil = new File(this.getFilesDir().getPath()+File.separator+"MyContactsBackUp");
        fil.mkdirs();

} else {
    File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"MyContactsBackUp");
    directory.mkdirs();
}

2 个答案:

答案 0 :(得分:4)

试试此代码

if (android.os.Environment.getExternalStorageState().equals(
    android.os.Environment.MEDIA_MOUNTED)) {

       File podcastFolder = new File(Environment.getExternalStorageDirectory() + File.separator
                                             + getString(R.string.app_name));
} else {
            /* save the folder in internal memory of phone */

    File podcastFolder = new File("/data/data/" + getPackageName()
                    + File.separator + getString(R.string.app_name));

}

答案 1 :(得分:2)

试试这个。

File f = new File(android.os.Environment.getExternalStorageDirectory(),File.separator+"MyContactsBackUp/");
f.mkdirs();

或用于应用程序的内部存储器,而不是使用写入硬核的sting。

File f = new File(getCacheDir(),File.separator+"MyContactsBackUp/");
f.mkdirs();