我创建了一个录音机应用程序,当我点击录制按钮时,我不知道为什么它会崩溃。
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "Audio_recorder_basic";
private MediaRecorder recorder = null;
private String path = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startRec(View view){
String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
path = Environment.getExternalStorageDirectory()+"/Seba/audiorecordtest.3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
Log.e(LOG_TAG, "prepare() failed");
}
recorder.start();
}
else{
Toast.makeText(getApplicationContext(), "SD not mounted", Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, "SD not mounted");
}
}
public void stopRec(View view){
recorder.stop();
recorder.release();
}
该应用程序只有2个按钮与android:onClick =" startRec"机器人:的onClick =" stopRec"
它还具有3个权限(写入和录制音频)。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.audiorecbas.audio_recorder_basic" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
从logcat path = /storage/emulated/0/Seba/audiorecordtest.3gp
这是来自Logcat的消息的一部分:
08-04 13:26:57.923 4216-4216/com.example.audiorecbas.audio_recorder_basic I/ViewRootImpl﹕ ViewRoot's Touch Event : ACTION_UP
08-04 13:26:57.941 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ java.io.IOException: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.943 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.File.createNewFile(File.java:946)
08-04 13:26:57.943 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.example.audiorecbas.audio_recorder_basic.MainActivity.startRec(MainActivity.java:37)
08-04 13:26:57.943 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.943 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.944 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$1.onClick(View.java:3829)
08-04 13:26:57.944 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View.performClick(View.java:4461)
08-04 13:26:57.944 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$PerformClick.run(View.java:18526)
08-04 13:26:57.944 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
08-04 13:26:57.945 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 13:26:57.945 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
08-04 13:26:57.945 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 13:26:57.945 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.945 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.945 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-04 13:26:57.947 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-04 13:26:57.947 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
08-04 13:26:57.947 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.948 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-04 13:26:57.948 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-04 13:26:57.949 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.File.createNewFile(File.java:939)
08-04 13:26:57.949 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ ... 15 more
08-04 13:26:57.949 4216-4216/com.example.audiorecbas.audio_recorder_basic E/Audio_recorder_basic﹕ create new file failed
08-04 13:26:57.949 4216-4216/com.example.audiorecbas.audio_recorder_basic E/Audio_recorder_basic﹕ /storage/emulated/0/Seba/audiorecordtest.3gp
08-04 13:26:57.977 4216-4216/com.example.audiorecbas.audio_recorder_basic V/MediaProfiles﹕ getInstance
08-04 13:26:57.977 4216-4216/com.example.audiorecbas.audio_recorder_basic V/MediaProfiles﹕ getLgeCamcorderCapParamByName: LgeCamcorderCap.audiozoomenable
08-04 13:26:57.977 4216-4216/com.example.audiorecbas.audio_recorder_basic E/MediaProfiles﹕ The mLgeCamcorderCap is not created, then return -1
08-04 13:26:57.977 4216-4216/com.example.audiorecbas.audio_recorder_basic D/MediaRecorder﹕ mAudioZoomEnable = -1
08-04 13:26:57.981 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Seba/audiorecordtest.3gp: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.984 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:458)
08-04 13:26:57.985 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
08-04 13:26:57.985 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
08-04 13:26:57.985 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
08-04 13:26:57.985 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.media.MediaRecorder.prepare(MediaRecorder.java:768)
08-04 13:26:57.985 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.example.audiorecbas.audio_recorder_basic.MainActivity.startRec(MainActivity.java:51)
08-04 13:26:57.985 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.986 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.986 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$1.onClick(View.java:3829)
08-04 13:26:57.986 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View.performClick(View.java:4461)
08-04 13:26:57.986 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$PerformClick.run(View.java:18526)
08-04 13:26:57.986 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
08-04 13:26:57.987 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 13:26:57.987 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
08-04 13:26:57.987 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 13:26:57.987 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.987 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.987 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-04 13:26:57.988 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-04 13:26:57.988 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
08-04 13:26:57.988 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.989 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-04 13:26:57.989 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-04 13:26:57.990 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:442)
08-04 13:26:57.990 4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ ... 19 more
08-04 13:26:57.990 4216-4216/com.example.audiorecbas.audio_recorder_basic E/Audio_recorder_basic﹕ prepare() failed
08-04 13:26:57.997 4216-4216/com.example.audiorecbas.audio_recorder_basic E/MediaRecorder﹕ start called in an invalid state: 4
08-04 13:26:58.001 4216-4216/com.example.audiorecbas.audio_recorder_basic D/AndroidRuntime﹕ Shutting down VM
08-04 13:26:58.001 4216-4216/com.example.audiorecbas.audio_recorder_basic W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d41d58)
08-04 13:26:58.004 4216-4216/com.example.audiorecbas.audio_recorder_basic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.audiorecbas.audio_recorder_basic, PID: 4216
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3834)
我使用LG L50进行开发。
答案 0 :(得分:0)
你有一个Java.io.FileNotFoundException:/storage/external_SD/Seba/audiorecordtest.3gp:open failed:ACCES(Permission denied)
我认为你必须使用
path = getFilesDir().getPath().concat("/storage/external_SD/Seba/audiorecordtest.3gp");
答案 1 :(得分:0)
添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
更改以下代码
if(status.equals("mounted")){
path = Environment.getExternalStorageDirectory()+"/yourfolderName/audiorecordtest.3gp";
File f = new File(path);
if(!f.exists()){
f.createNewFile();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
Log.e(LOG_TAG, "prepare() failed");
}
recorder.start();
}