【问题标题】:Change default location of voice recorder files更改录音机文件的默认位置
【发布时间】:2015-09-09 23:11:00
【问题描述】:

我正在使用我的 Android 应用程序中的内置录音机。有没有办法可以更改录制文件的默认位置?它们存储在 SD 卡的“声音”文件夹中。我想将它们存储在不同的文件夹中。如果可能的话,我该怎么做?

下面是调用内置Android录音机App的简单代码。

Button startRecording = (Button) findViewById(R.id.startBtn);

    startRecording.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            Intent recordIntent = new Intent(
                    MediaStore.Audio.Media.RECORD_SOUND_ACTION); 

            startActivityForResult(recordIntent, REQUEST_CODE_RECORD);

        }
    });

【问题讨论】:

    标签: android audio-recording


    【解决方案1】:

    你必须允许使用默认位置和文件名记录文件,然后移动文件。 你可以在你的 onActivityResult 中做这样的事情:

       @Override protected void onActivityResult(
             int requestCode, int resultCode, Intent data) {
          switch (requestCode) {
          case REQUEST_CODE_RECORD:
             if (resultCode == Activity.RESULT_OK) {
                // Sound recorder does not support EXTRA_OUTPUT
                Uri uri = data.getData();
                try {
                   String filePath = getAudioPathFromUri(uri);
                   copyFile(filePath);
                   getContentResolver().delete(uri, null, null);  
                   (new File(filePath)).delete();
                } catch (IOException e) {
                   throw new RuntimeException(e);
                }
             }
          }
       }
    
       private String getAudioPathFromUri(Uri uri) {
          Cursor cursor = getContentResolver()
                .query(uri, null, null, null, null);
          cursor.moveToFirst();
          int index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
          return cursor.getString(index);
       }
    
       private void copyFile(String fileName) throws IOException {
          Files.copy(new File(fileName), 
             new File(Environment.getExternalStorageDirectory(), fileName));
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多