首先你的应用必须有写外部存储的权限,它在manifest文件中指定,请看下面:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
其次,我们应该检查是否安装了外部媒体,请查看我的一个 Android 应用程序的工作片段,如果外部存储不存在,它将为您创建一个目录,否则返回该目录的 File 对象。
File getRecordingDirectory()
{
File recordingDir = null;
// get the state of external storage
String externalStorageState = Environment.getExternalStorageState();
if(externalStorageState.equals(Environment.MEDIA_MOUNTED))
{
File extDir =
Environment.getExternalStorageDirectory();
recordingDir = new File(extDir, "AudioRecorder");
if(!recordingDir.exists())
{
if(!recordingDir.mkdirs())
{
Log.e("RECORDER", "Unable to create Silent Recorder recordings directory.");
}
}
}
return recordingDir;
}