1 つの録音アプリケーションを作成しています。
音声を録音して SDカードに保存する必要があります。しかし、アプリケーションを実行しようとすると正常に実行されますが、停止ボタンを押すと、「残念ながらアプリケーションが停止されました」というメッセージが表示され
ます
enter code here
08-01 12:09:18.331: E/SoundRecordingActivity(5611): sdcard access error
08-01 12:09:21.043: D/AndroidRuntime(5611): Shutting down VM
08-01 12:09:21.043: W/dalvikvm(5611): threadid=1: thread exiting with uncaught exception (group=0x2c3381f8)
08-01 12:09:21.043: E/AndroidRuntime(5611): FATAL EXCEPTION: main
08-01 12:09:21.043: E/AndroidRuntime(5611): java.lang.NullPointerException
08-01 12:09:21.043: E/AndroidRuntime(5611): at com.blitze.recordingapp.SoundRecordingActivity$2.onClick(SoundRecordingActivity.java:110)
08-01 12:09:21.043: E/AndroidRuntime(5611): at android.view.View.performClick(View.java:3511)
08-01 12:09:21.043: E/AndroidRuntime(5611): at android.view.View$PerformClick.run(View.java:14115)
08-01 12:09:21.043: E/AndroidRuntime(5611): at android.os.Handler.handleCallback(Handler.java:605)
08-01 12:09:21.043: E/AndroidRuntime(5611): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 12:09:21.043: E/AndroidRuntime(5611): at android.os.Looper.loop(Looper.java:137)
08-01 12:09:21.043: E/AndroidRuntime(5611): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-01 12:09:21.043: E/AndroidRuntime(5611): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 12:09:21.043: E/AndroidRuntime(5611): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 12:09:21.043: E/AndroidRuntime(5611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 12:09:21.043: E/AndroidRuntime(5611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 12:09:21.043: E/AndroidRuntime(5611): at dalvik.system.NativeStart.main(Native Method)
08-01 12:09:23.759: W/System.err(5611): java.lang.Throwable: stack dump
08-01 12:09:23.759: W/System.err(5611): at java.lang.Thread.dumpStack(Thread.java:496)
08-01 12:09:23.759: W/System.err(5611): at android.os.Process.killProcess(Process.java:725)
08-01 12:09:23.759: W/System.err(5611): at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:82)
08-01 12:09:23.759: W/System.err(5611): at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
08-01 12:09:23.759: W/System.err(5611): at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
08-01 12:09:23.759: W/System.err(5611): at dalvik.system.NativeStart.main(Native Method)
これが私のコードです
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startButton.setEnabled(false);
stopButton.setEnabled(true);
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound", ".3gp", sampleDir);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
recorder.stop();
recorder.release();
//counter.cancel();
addRecordingToMediaLibrary();
}
});
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
}
SDカードを携帯につけました。何が問題なのかわかりません。何かヒントや参考になれば。
ありがとうございました..