【发布时间】:2013-08-16 17:29:37
【问题描述】:
我正在制作一个录音机应用程序。该应用程序应该记录文件并将它们保存在外部存储目录中。这就是我正在尝试的方式:
声明一些全局变量:
String externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
String outputPath = externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/no_name.mp3"; //output path for mp3 files
File appDirectory = new File(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad"); //this is the directory I want to create when app is started for the first time
很少登录 onCreate 来检查 externalStorage 是否正常并正常工作:
Log.i("TAG", "External storage directory: " + externalStoragePath);
Log.i("TAG", "External storage state: " + Environment.getExternalStorageState());
日志显示外部存储完全正常 (storage/emulated/0) 并且状态为 Mounted。
如果目录还不存在,我会尝试创建它:
if (!appDirectory.exists())
{
try {
appDirectory.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我知道如果设备通过 USB 电缆连接到 PC,则外部存储无法正常工作,因此我从 USB 中移除了设备并运行应用程序,但未按应有的方式创建文件夹。基本上什么都没有发生。
【问题讨论】:
-
使用 appDirectory.mkdir() 代替 appDirectory.createNewFile() 尝试告诉我。