你这样做:
//use getExternalStorageState to determine if there is an sd card
if (Environment.getExternalStorageState() == null) {
directory = new File(Environment.getDataDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getDataDirectory()
+ "/Robotium-Screenshots/");
/*
* this checks to see if there are any previous test photo files
* if there are any photos, they are deleted for the sake of
* memory
*/
//if the directory exists, delete files in directory
if (photoDirectory.exists()) {
File[] dirFiles = photoDirectory.listFiles();
if (dirFiles.length != 0) {
for (int ii = 0; ii <= dirFiles.length; ii++) {
dirFiles[ii].delete();
}
}
}
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
// if phone DOES have sd card
} else if (Environment.getExternalStorageState() != null) {
// search for directory on SD card
directory = new File(Environment.getExternalStorageDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(
Environment.getExternalStorageDirectory()
+ "/Robotium-Screenshots/");
if (photoDirectory.exists()) {
File[] dirFiles = photoDirectory.listFiles();
if (dirFiles.length > 0) {
for (int ii = 0; ii < dirFiles.length; ii++) {
dirFiles[ii].delete();
}
dirFiles = null;
}
}
// if no directory exists, create new directory to store test
// results
if (!directory.exists()) {
directory.mkdir();
}
}
这是一个非常简单的过程。我在这里所做的是检查是否有 SD 卡,如果有,我搜索目录,如果没有目录,我创建一个。如果有目录并且有内容,我会删除它。你不必做所有这些。如果你喜欢,你可以使用Environment.getExternalStorageDirectory()
. 这将获得 SD 卡。从那里你创建一个bufferedWriter
并写。您还需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单中添加。如果您有任何问题,请告诉我。
我希望这有帮助!