8

Android 11 对文件存储和访问进行了多项更改。显然其中之一是不能再将输出定位到“/dev/null”(我的场景实际上在这个老问题中得到了准确的解释)。

尽管引用的问题解决了特定问题,但有一件事仍未得到解答:Android 11 与“/dev/null”的等价物是什么。也就是说,如果不需要特定操作的输出(在我们的例子中,它是一个创建大文件的操作)。

4

2 回答 2

2

Eventually I ended up solving my problem the following way (answer tailored to MediaRecorder problem but can be generalized to other situations too):

fun MediaRecorder.setOutputFile(context: Context) {
    val tmpRecordingFolder = File(context.filesDir, "tmp_media")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        setOutputFile(File(tmpRecordingFolder, "recording.mp3"))
    } else {
        setOutputFile("/dev/null")
    }
}

Basically I am setting the output to be in the internal storage. I hope the file will not get huge and I am deleting the file in as many places in the code as possible. This seems to work on newer devices, currently have not yet ran into storage problems either, but the solution is not rolled out to production yet. Will update my answer if problems are identified.

于 2021-03-05T11:18:41.567 回答
0

我有同样的问题,如果你不提供它,你必须指定一个路径,因为 MediaRecorder 在 Android 11 中崩溃,为了避免写入大量文件,你可以尝试通过停止/重新启动 MediaRecorder 来刷新文件,我这几天也一直在处理这个问题。

我在这里回复了一个更详细的答案:MediaRecorder Android 11 start failed -1004

于 2021-11-16T16:46:12.173 回答