【问题标题】:Save an audio file into a specific folder in android将音频文件保存到android中的特定文件夹中
【发布时间】:2014-11-04 23:42:35
【问题描述】:

嗯,首先我是 android 开发的新手,我正在自学。我按照一些教程制作了一个录音机应用程序。但在这些教程中,将录制的文件保存在路径 /sdcard 中。我希望我的应用程序保存到另一个位置。例如在 /sdcard/Voice 路径中(未创建语音文件夹)。最简单的方法是如何做到的?我搜索了很多,但找不到任何答案。

我的代码(请理解我的母语是西班牙语,所以一些方法是西班牙语)

public class FragmentRecording extends Fragment {
private MediaRecorder recorder;
private String OUTPUT_FILE;
FloatingActionButton stop_fab;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    View view=inflater.inflate(R.layout.fragment_recording, container,false);
    stop_fab=(FloatingActionButton)view.findViewById(R.id.stop_fab_xml);
    //SALIDA DEL ARCHIVO
    OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/VoiceRecorder.3gpp";

    //GENERAMOS UN METODO PARA SABER LIBERAR EL MICROFONO
    LiberarMicro();
    //Crea el archivo
    File outfile=new File(OUTPUT_FILE);
    //Si existe lo borra
    if(outfile.exists()){
        outfile.delete();
    }
    //Empieza Proceso de Grabacion
    try {
        StartRecord();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    stop_fab.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            StopRecord();
        }

    });


    return view;}



protected void StopRecord() {
    // TODO Auto-generated method stub
    if(recorder!=null){
        recorder.stop();
    }
}



private void StartRecord() throws IllegalStateException, IOException {
    // TODO Auto-generated method stub
    recorder=new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);

    recorder.prepare();

    recorder.start();
}

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

}}

【问题讨论】:

    标签: android record voice


    【解决方案1】:
    File f = new File(Environment.getExternalStorageDirectory() + "/somedir");
    if(f.isDirectory()) {
            //Write code for the folder exist condition
    
    }else{
    
            // create a File object for the parent directory
            File wallpaperDirectory = new File("/sdcard/Wallpaper/");
            // have the object build the directory structure, if needed.
            wallpaperDirectory.mkdirs();
           // create a File object for the output file
           File outputFile = new File(wallpaperDirectory, filename);
           // now attach the OutputStream to the file object, instead of a String representation
           FileOutputStream fos = new FileOutputStream(outputFile);
    
    }
    

    上面的代码会检查文件夹是否存在。如果不创建文件夹并将文件保存到该文件夹​​。

    您需要在清单中提供以下权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    如果您感到困惑,请告诉我。如果我有帮助,请投票 :) 快乐编码

    【讨论】:

      【解决方案2】:

      使用此代码

        SDCardpath = getFilesDir();
          myDataPath = new File(SDCardpath.getAbsolutePath()
                  + "/.My Recordings");
      
          // mydir = context.getDir("media", Context.MODE_PRIVATE);
          if (!myDataPath.exists())
              myDataPath.mkdir();
      
      
          audiofile = new File(myDataPath + "/" + fileName);
      
                           OR 
      

      授予存储权限

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      

      用这个作为sd卡的路径

      File sdcard = Environment.getExternalStorageDirectory();
      File storagePath = new File(sdcard.getAbsolutePath() + "/folderName");
      File yourfile= new File(storagePath + "/" + fileName + ".filetype");
      

      【讨论】:

        【解决方案3】:

        只需先将 /Voice 添加到您的 OUTPUT_FILE。然后添加以下代码:

         if (!outfile.getParentFile().exists())
            outfile.getParentFile().mkdirs();
        

        【讨论】:

          【解决方案4】:

          您也可以使用此代码。

             boolean exists = (new File(path)).exists();
               if (!exists){new File(path).mkdirs();}
          

          它允许在 sd 卡中创建路径,或者您可以按照完整的tutorial

          【讨论】:

            猜你喜欢
            • 2021-12-28
            • 2014-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多