我正在尝试将输出音频文件保存在一个单独的新文件夹中,但是当我尝试启动应用程序时它会崩溃:
我的文件创建代码如下:
在 onCreate() aof mainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button)findViewById(R.id.button3);
stop=(Button)findViewById(R.id.button2);
record=(Button)findViewById(R.id.button);
stop.setEnabled(false);
play.setEnabled(false);
final File path =
Environment.getExternalStoragePublicDirectory
(
//Environment.DIRECTORY_PICTURES
//Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DCIM + "/Utkarshrecord/"
);
// Make sure the sound directory exists.
if(!path.exists())
{
path.mkdirs();
}
try {
file.createTempFile("sound", ".3gp", path);
myAudioRecorder=new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setOutputFile(file.getAbsolutePath());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
记录 停止和播放有三个按钮:
记录的代码:record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
代码停止:
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
try{
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
}catch(Exception e){
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
stop.setEnabled(false);
play.setEnabled(true);
}
});
错误如下:
10-05 14:44:04.910: E/AndroidRuntime(31147): FATAL EXCEPTION: main
10-05 14:44:04.910: E/AndroidRuntime(31147): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sensorlist/com.example.sensorlist.MainActivity}: java.lang.NullPointerException
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.os.Handler.dispatchMessage(Handler.java:107)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.os.Looper.loop(Looper.java:194)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.main(ActivityThread.java:5371)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.lang.reflect.Method.invoke(Method.java:525)
10-05 14:44:04.910: E/AndroidRuntime(31147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-05 14:44:04.910: E/AndroidRuntime(31147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-05 14:44:04.910: E/AndroidRuntime(31147): at dalvik.system.NativeStart.main(Native Method)
10-05 14:44:04.910: E/AndroidRuntime(31147): Caused by: java.lang.NullPointerException
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.File.fixSlashes(File.java:185)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.File.<init>(File.java:134)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
10-05 14:44:04.910: E/AndroidRuntime(31147): at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
10-05 14:44:04.910: E/AndroidRuntime(31147): at com.example.sensorlist.MainActivity.onCreate(MainActivity.java:64)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.Activity.performCreate(Activity.java:5122)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
10-05 14:44:04.910: E/AndroidRuntime(31147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
10-05 14:44:04.910: E/AndroidRuntime(31147): ... 11 more
我应该怎么做才能删除它?
答案 0 :(得分:2)
因为你所犯的一些错误非常愚蠢!
错误1:
file.createTempFile("sound", ".3gp", path);
对于static
参考,它应该是
File.createTempFile("sound", ".3gp", path);
它应该存储在文件中,直到现在为null
!
第二个错误:
播放按钮中的: 将其更改为
m.setDataSource(file.getAbsolutePath());