Android SD卡中的文件创建不工作

Android SD卡中的文件创建不工作,android,Android,我正在尝试创建一个文件并将其存储在SD卡中,作为应用程序某些处理的输入。 在搜索了一段时间后,我得到了这个可以在SD卡中创建文件的代码。但是在运行这个之后,我看不到在我的SD卡中创建的任何文件。谁能帮帮我,我这里缺什么 BufferedWriter out = new BufferedWriter(new FileWriter(FileDescriptor.err)); try { File root = Environment.getExternalStorageDirectory(

我正在尝试创建一个文件并将其存储在SD卡中,作为应用程序某些处理的输入。 在搜索了一段时间后,我得到了这个可以在SD卡中创建文件的代码。但是在运行这个之后,我看不到在我的SD卡中创建的任何文件。谁能帮帮我,我这里缺什么

BufferedWriter out = new BufferedWriter(new FileWriter(FileDescriptor.err));

try {
    File root = Environment.getExternalStorageDirectory();
    if (root.canWrite()) {
    File perffile = new File(root, "samplefile.txt");
    FileWriter perfwriter = new FileWriter(perffile, true);
    out = new BufferedWriter(perfwriter);
    }
} catch (IOException e) {
    Log.e(TAG, "-Could not write file " + e.getMessage());
    return; 
}

如果要添加文件或文件夹或将应用程序移动到SD卡中,只需执行以下操作:

步骤:

1) 使用文本或编程编辑器打开Android应用程序的源代码文件

2) 浏览到源代码中希望调用将文件写入设备外部存储器的函数的位置

3) 插入这一行代码以检查SD卡:

File sdCard = Environment.getExternalStorageDirectory();
FileOutputStream f = new FileOutputStream(file);
Finally step 7:
4) 插入以下代码行以设置目录和文件名:

File dir = new File (sdcard.getAbsolutePath() + "/folder1/folder2");
dir.mkdirs();
File file = new File(dir, "example_file");

// The mkdirs funtion will create the directory folder for you, use it only you want to create a new one.
5) 将上述代码中的“/folder1/folder2”替换为要保存文件的实际路径。这应该是您通常保存应用程序文件的位置。另外,将“example_file”值更改为您希望使用的实际文件名

6) 插入以下代码行以将文件输出到SD卡:

File sdCard = Environment.getExternalStorageDirectory();
FileOutputStream f = new FileOutputStream(file);
Finally step 7:
保存文件,然后编译并使用Android emulator软件或设备测试应用程序


这会有用的!!!;-)

您实际上还没有向文件中写入任何内容。不确定FileWriter的内部结构,但我不认为在不向其写入内容的情况下会创建此文件…我用此编写了一个小型桌面java程序,在linux上,我可以确认它确实使用此代码创建了此文件。您是否在清单中请求了权限?是的,我确实请求过你们在清单文件中提到的许可。我正在Windows 7上工作。我收到以下消息:ActivityManager(525):Process com.example.samplejni(pid 8016)已死亡。