从SDCard获取录制的文件并将其附加到电子邮件客户端

时间:2013-12-17 12:15:21

标签: android file audio path

我已经在我的应用程序中实现了录音机功能。现在我需要获取文件并将其附加到GMailSender。如何获取文件。这是我尝试启动视频的代码。有一个保存按钮在我的用户界面中,当它被点击时,音频文件将自动附加到邮件中。

private void startRecord()
{ 
    File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
    try
    {
        file.createNewFile();
        OutputStream outputStream = new FileOutputStream(file);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);

        int minBufferSize = AudioRecord.getMinBufferSize(8000,
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);

        short[] audioData = new short[minBufferSize];

        AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT,
                            minBufferSize);

        audioRecord.startRecording();

        while(recording)
        {
            int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
            for(int i = 0; i < numberOfShort; i++)
            {
                dataOutputStream.writeShort(audioData[i]);
            }
        }
        audioRecord.stop();
        audioRecord.release();
        dataOutputStream.close();

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

}

请有人帮助我!

1 个答案:

答案 0 :(得分:0)

使用以下代码发送电子邮件...只需在onCreate方法中使用此代码即可完成发送邮件。

   String subject = "Your subject name";
   String message = "Message "

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("audio/*");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
            "email-id"});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    Uri uri = Uri.fromFile(new File(Environment
            .getExternalStorageDirectory(), "path of audio file"));
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    emailIntent.setType("text/plain");
    startActivity(emailIntent);