录音机的状态无效(错误的FilePath?)

时间:2018-11-11 05:48:46

标签: java android-studio

我正在尝试制作一个录音机,以对其进行测试并查看其工作原理。但是,当我运行它时,出现错误:

E/MediaRecorder: start called in an invalid state: 4

因此,我做了一些googlejitzu并发现我的文件路径错误。这是我的代码:

final MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile("Test");

    System.out.println("Hello");
    try {
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();

    }
    recorder.start();
    CountDownTimer countDownTimer = new CountDownTimer(30000, 1000) {
        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            recorder.stop();
        }
    }.start();

所以我想我想知道如何在电话上建立文件路径目录以将音频保存到其中。

错误是:

    11-11 01:49:48.275 23703-23703/com.example.arege.dayatalisten W/System.err: java.io.FileNotFoundException: Test: open failed: EROFS (Read-only file system)
11-11 01:49:48.276 23703-23703/com.example.arege.dayatalisten W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:455)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
        at android.media.MediaRecorder.prepare(MediaRecorder.java:834)
        at com.example.arege.dayatalisten.ListeningToTheWorld.onStartCommand(ListeningToTheWorld.java:47)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3432)
        at android.app.ActivityThread.-wrap21(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6316)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
    Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
11-11 01:49:48.277 23703-23703/com.example.arege.dayatalisten W/System.err:     at libcore.io.Posix.open(Native Method)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
        at libcore.io.IoBridge.open(IoBridge.java:441)
        ... 13 more

我拥有两个必需的权限。

1 个答案:

答案 0 :(得分:1)

您正在尝试写入应用程序无权访问的内部存储。它不是根设备。

您需要将文件写入外部存储。

使用以下任一方法:getFilesDir() or getExternalFilesDir()(在上下文或活动上)获取目录并将文件写入该位置。

伪代码:

File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();