Android的发送记录的音频文件作为电子邮件的附件试图读取文件时,给错误(URI路径不是绝对) [英] Sending android audio recorded file as an attachment of an email giving error when trying to read file (URI path not absolute)

查看:222
本文介绍了Android的发送记录的音频文件作为电子邮件的附件试图读取文件时,给错误(URI路径不是绝对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我节省使用介质记录音频文件和下面的code:

I'm saving an audio file using Media recorder and the following code:

 public class AudioRecorder {

 final MediaRecorder recorder = new MediaRecorder();
 final String path;


  /**
   * Creates a new audio recording at the given path
   */
  public AudioRecorder(String path) {
  this.path = sanitizePath(path);
}

public static String sanitizePath(String path) {
   if (!path.startsWith("/")) {
      path = "/" + path;
   }
   if(path.endsWith("/")){
      path = path + "/";
  }
  return path;
 }

 /**
  * Starts a new recording.
  */
  public void start() throws IOException {
  // make sure the directory we plan to store the recording in exists
  File directory = new File(path).getParentFile();
  if (!directory.exists() && !directory.mkdirs()) {
  throw new IOException("Path to file could not be created.");
 }

   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
   recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
   recorder.setOutputFile(path);
   recorder.prepare();
   recorder.start();
  }

 /**
  * Stops a recording that has been previously started.
  */
 public void stop() throws IOException {
 recorder.stop();
 recorder.release();
 }
}

我然后使用以下code调用这个类

I am then calling this class using the following code

 int timeOfRecroding = AppPrefs.getSettingsAdditionalTimeOfRecording() * 60 * 1000;
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("media", Context.MODE_PRIVATE);

    final String pathAndName = AudioRecorder.sanitizePath(directory.getAbsolutePath() + "/LocRec.3gp");
    final AudioRecorder audioRecorder = new AudioRecorder(pathAndName);

    if(Constants.isTest){
        showToast("Starting recording for [" + AppPrefs.getSettingsAdditionalTimeOfRecording() + "] minutes");
        showToast("Recording to path: [" + pathAndName + "]");

    }

和然后当然使用
     audioRecorder.start();

     audioRecorder.stop();
做实际的记录

and then of course using audioRecorder.start(); and audioRecorder.stop(); do the actual recording

在录制正在使用相同的pathAndName获取文件,并使用以下code得到的文件将其作为附件在电子邮件中完成我

After the recording is done I using the same pathAndName to get the file and send it as attachment in an email using the following code to get the file

new File(new URI(AppPrefs.getInfoToSend(Constants.SERVICE_CODE_SEND_RECORDING, Constants.MESSAGE_TYPE_EMAIL)))

但这引发错误时抛出

but this is throwing an excpetion

URI is not absolute: /data/data/com.testrecoding.record/app_media/LocRec.3gp

我AP preciate任何帮助,
谢谢,
Wassim

I appreciate any help, Thanks, Wassim

推荐答案

这听起来很蠢,但我发现了一个掉头,不是一个真正的周转,我不知道如何错过了摆在首位,得到了从文件的路径上,无需使用URI

it may sound stupid, but I found a turn-around, not really a turn-around, not sure how I missed it in the first place, getting the file from the path directly without using URI

 new File("PathOfFIle")

这篇关于Android的发送记录的音频文件作为电子邮件的附件试图读取文件时,给错误(URI路径不是绝对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆