I am trying to save output audio file in a separate new folder but when I try to start the app it crashes :
The code of my file creation is as follows:
In 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();
}
There are three buttons for record stop and play:
code for record:
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();
}
});
code for stop:
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);
}
});
the error is as follows:
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
what should I do to remove it ?
For there are some mistakes which u have done which are very silly !
Mistake 1:
file.createTempFile("sound", ".3gp", path);
For static
ref it should be
File.createTempFile("sound", ".3gp", path);
and it should be stored it in your file which is null
till now !
2nd Mistake :
in Play button : change it to
m.setDataSource(file.getAbsolutePath());