【发布时间】:2014-12-24 08:04:19
【问题描述】:
我想在 sdcard 的文件夹中创建一个文本文件,然后在该文件中写入一些数据。我检查了很多页面,例如:
Saving to SD card as text file
Write file to sdcard in android
我使用此代码创建文件和文件夹:
try {
File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
if (!newFolder.exists()) {
newFolder.mkdir();
}
Log.d(TAG, "TestFolder");
try {
file = new File(newFolder, "MyTest" + ".txt");
file.createNewFile();
Log.d(TAG, "MyTest");
} catch (Exception ex) {
System.out.println("ex: " + ex);
}
} catch (Exception e) {
System.out.println("e: " + e);
}
但是它会在手机内存而不是 sdcard 中创建文件。我使用了这些权限: 然后我用这段代码在同一个文本文件中写了一个小字符串:
String str = "my_string";
FileOutputStream outStream;
try {
outStream = openFileOutput(file.getName(), Context.MODE_PRIVATE);
outStream.write(str.getBytes());
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但它不会在文本文件文件中写入任何内容。 我的代码有什么问题?
顺便说一下,我也使用了这些权限:READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE",READ_INTERNAL_STORAGE",WRITE_INTERNAL_STORAGE"
所以这不是许可的问题。
【问题讨论】:
-
你用的是什么安卓版本?
-
我使用姜饼进行开发,但我在两个使用 4.2.2 和 kitkat 的设备上检查我的应用程序
-
在 4.2.2 和 kitkat 设备上都出现问题??
-
是的!!!我都检查了。