e/mediarecording:外部存储访问错误

fhity93d  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(255)

1.您好,我的媒体录像机有点小问题,更确切地说,是访问存储器的问题。我每次收到访问问题的消息时都知道我已经在清单中定义了权限。我需要将备忘录保存在一个文件中,以便以后重用,为此,我在代码中编写了一个外部存储器。如果有人能帮我找出问题的根源。当我开始录音的时候,信息就来了。
我只需要录制一个声音并保存在文件中。谢谢您

  1. package com.example.myapplication4;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.content.ContentResolver;
  6. import android.content.ContentValues;
  7. import android.content.Intent;
  8. import android.media.MediaRecorder;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.provider.MediaStore;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.Toast;
  17. public class MainActivity extends Activity {
  18. MediaRecorder recorder;
  19. File auDefile = null;
  20. static final String TAG = "MediaRecording";
  21. Button startButton,stopButton;
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. startButton = (Button) findViewById(R.id.button1);
  27. stopButton = (Button) findViewById(R.id.button2);
  28. }
  29. public void startRecording(View view) throws IOException {
  30. startButton.setEnabled(false);
  31. stopButton.setEnabled(true);
  32. `Creating file`
  33. File dir = Environment.getExternalStorageDirectory();
  34. try {
  35. auDefile = File.createTempFile("sound", ".3gp", dir);
  36. } catch (IOException e) {
  37. Log.e(TAG, "external storage access error");
  38. return;
  39. }
  40. recorder = new MediaRecorder();
  41. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  42. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  43. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  44. recorder.setOutputFile(auDefile.getAbsolutePath());
  45. recorder.prepare();
  46. recorder.start();
  47. }
  48. public void stopRecording(View view) {
  49. startButton.setEnabled(true);
  50. stopButton.setEnabled(false);
  51. `stopping recorder`
  52. recorder.stop();
  53. recorder.release();
  54. addRecordingToMediaLibrary();
  55. }
  56. protected void addRecordingToMediaLibrary() {
  57. ContentValues values = new ContentValues(4);
  58. long current = System.currentTimeMillis();
  59. values.put(MediaStore.Audio.Media.TITLE, "audio" + auDefile.getName());
  60. values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
  61. values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
  62. values.put(MediaStore.Audio.Media.DATA, auDefile.getAbsolutePath());
  63. ContentResolver contentResolver = getContentResolver();
  64. Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  65. Uri newUri = contentResolver.insert(base, values);
  66. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
  67. Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
  68. }
  69. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题