如何指定输出文件的文件夹调用RECORD_SOUND_ACTION什么时候?什么时候、文件夹、文件、RECORD_SOUND_ACTION

2023-09-05 04:17:56 作者:人与狗??

我想,当我打电话到指定目标文件夹中的 MediaStore 内置的录音机应用程序,如在SD卡的文件夹。根据 Android的文档,有一个的 EXTRA_OUTPUT 调用时 RECORD_SOUND_ACTION

I'd like to specify a destination folder when I call the MediaStore built-in sound recorder app, e.g. the sdcard folder. According to the Android documentation, there is no support for EXTRA_OUTPUT when calling the RECORD_SOUND_ACTION.

我怎样才能做到这一点?

How can I do this?

推荐答案

您必须允许文件使用任何默认的文件名和位置使用,然后将文件记录。移动文件是远离微不足道。这里有一个完整的例子。

You will have to allow the file to be recorded using whatever default filename and location are used and then move the file. Moving the file is far from trivial. Here's a complete example.

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.google.common.io.Files;

public class SoundRecorder extends Activity {
   private static final int RECORD_QUESTION_SOUND_REQUEST_CODE = 1;

   @Override protected void onResume() {
      super.onResume();
      Intent recordIntent = new Intent(
            MediaStore.Audio.Media.RECORD_SOUND_ACTION);
      // NOTE: Sound recorder does not support EXTRA_OUTPUT
      startActivityForResult(recordIntent, RECORD_QUESTION_SOUND_REQUEST_CODE);
   }

   @Override protected void onActivityResult(
         int requestCode, int resultCode, Intent data) {
      switch (requestCode) {
      case RECORD_QUESTION_SOUND_REQUEST_CODE:
         if (resultCode == Activity.RESULT_OK) {
            // Sound recorder does not support EXTRA_OUTPUT
            Uri uri = data.getData();
            try {
               String filePath = getAudioFilePathFromUri(uri);
               copyFile(filePath);
               getContentResolver().delete(uri, null, null);  
               (new File(filePath)).delete();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
      }
   }

   private String getAudioFilePathFromUri(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));
   }
}

注: com.google.common.io.Files.copy()是番石榴的文件副本;随意使用替代实现或编写您自己的Java文件复印机。

NOTE: com.google.common.io.Files.copy() is from Guava's file copy; feel free to use an alternate implementation or write your own Java file copier.

 
精彩推荐
图片推荐