-6

这是将资产复制到 SD 卡的代码

但是,它只是将文件复制到根目录。如果我将代码更改为此,则文件将被复制到 mydirectory,但条件是必须先手动创建“mydirectory”:

Environment.getExternalStorageDirectory() + "/mydirectory/"
                                    + files[i]);

我的问题是我应该如何修改代码以便可以创建一个新目录?我发现了这个并遇到了创建文件对象并使用mkdirs()

但是我真的不知道该怎么做,因为我是 android 开发的新手并且没有编程方面的先验知识。

如果有人可以逐步指导我如何实现这一点,我将不胜感激。

谢谢你。

4

2 回答 2

1

您必须在开始添加文件之前创建目录(显然)。

在您尝试复制所谓的“资产”的文件中使用它。见评论:

String filename = Environment.getExternalStorageDirectory() + "/mydirectory/"); // this sets the filename of the new dir
File newDir = new File (filename); // this creates the File object (no directory yet)
if (!newDir.exists ())  // here we check whether the dir is already there
    newDir.mkdirs ()   // if not, then create all necessary directories and subdirectories to our new dir 
于 2013-03-21T13:29:47.227 回答
0

它可能会帮助你..

boolean success = (new File("/sdcard/dirname")).mkdir(); 
if (!success)
{
    Log.w("directory not created", "directory not created");
}
于 2013-03-21T14:33:59.037 回答