我创建了一个简单的应用程序来获取一些用户数据并将其写入文本文件,该文件将保存在我的设备的外部存储器中。但是,在重新启动设备之前,我无法使用计算机访问这些文件。任何人都可以告诉我为什么会这样,如果有什么我可以做的来解决它?
这是我用来写数据的代码。
private void commitToFile(String worldOrApp, String xPos, String yPos, String orient) {
Intent intent = getIntent();
String filename = intent.getStringExtra(MainActivity.FILENAME) + ".txt";
final String position = worldOrApp + " - x: " + xPos + "; y: " + yPos + "; alpha: " + orient + "\r\n";
File myPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File myFolder = new File(myPath.getAbsolutePath()+"/test_folder");
if (!myFolder.exists()) {
myFolder.mkdirs();
}
File myFile = new File(myFolder, filename);
try {
FileOutputStream fileOutputStream = new FileOutputStream(myFile, true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.write(position);
outputStreamWriter.flush();
outputStreamWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
感谢@CommonsWare的指导。我在Android saving file to external storage
找到了以下代码// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
我直接放在我的代码中的异常catch下面,并将文件更新到myFile,这是我的commitToFile方法的相关文件。