0

无论如何,当我使用 sdcard 中的 OUTPUT_FILE 路径按下保存按钮时,我是否可以发送电子邮件。(我还没有实现保存按钮)我应该将 String 更改为 Uri 来发送电子邮件吗?我不知道应该如何实现保存按钮,使其发送带有音频文件附件的电子邮件。有什么建议么?提前致谢!

public class FulfillAudioTaskActivity extends Activity {

    private MediaPlayer mediaPlayer;
    private MediaRecorder recorder;
    private String OUTPUT_FILE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fulfill_audio_task);



        OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp";

    }

    public void buttonTapped(View view){
        switch(view.getId()) {
        case R.id.startBtn:
            try {
                beginRecording();           
            }catch (Exception e){
                e.printStackTrace();
            }
            break;
        case R.id.finishBtn:
            try {
                stopRecording();

            } catch (Exception e){
                e.printStackTrace();
            }
            break;
        case R.id.playBtn:
            try {
                playRecording();

            } catch (Exception e){
                e.printStackTrace();
            }
            break;
        case R.id.stopBtn:
            try {
                stopPlayback();

            } catch (Exception e){
                e.printStackTrace();
            }
            break;
        }
    }

    private void stopPlayback() {
        if(mediaPlayer != null)
            mediaPlayer.stop();

    }

    private void playRecording() throws Exception{
        ditchMediaPlayer();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(OUTPUT_FILE);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
    private void ditchMediaPlayer() {
        if(mediaPlayer != null)
        {
            try {
                mediaPlayer.release();

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    // stop recording if there's a recorder running
    private void stopRecording() {

        if (recorder != null)
            recorder.stop();
    }

    private void beginRecording() throws Exception {

        ditchMediaRecorder();
        File outFile = new File(OUTPUT_FILE);

        //check if there's a file already recorded, and if it is we want to get rid of it.
        if(outFile.exists())
            outFile.delete();

        //create a new MediaRecorder object.
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder.setOutputFile(OUTPUT_FILE);
        recorder.prepare();
        recorder.start();

    }

    private void ditchMediaRecorder() {
        // TODO Auto-generated method stub
        if(recorder != null)
            recorder.release();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_fulfill_audio_task, menu);
        return true;
    }
}
4

1 回答 1

2
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("audio/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] {"someone@gmail.com"} );
    i.putExtra(Intent.EXTRA_SUBJECT, "MySubject");
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "audiorecorder.3gpp");
    startActivity(i);

希望这可以帮助!

于 2012-11-29T06:37:22.717 回答