要将文件存储在SD卡上,在代码中需要指定路径。以下是将文件存储在SD卡上的示例代码:
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myDirectory/myFile.txt";
File file = new File(filePath);
FileOutputStream fos = null;
try {
if (file.createNewFile()) {
fos = new FileOutputStream(file);
String content = "This is the content that will be written to the file.";
fos.write(content.getBytes());
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
在上述示例中,Environment.getExternalStorageDirectory().getAbsolutePath()
方法返回SD卡的路径。通过使用此路径和指定的目录和文件名,可以将文件存储在SD卡上。