我正在创建要通过Android MediaMuxer进行录制的文件,并且希望将此文件存储在我在可移动SD卡中指定的任何文件夹中(不仅在/ Android / data / data / application中)。
如果Android API级别高于26,我可以使用SAF(存储访问框架)来实现它。
var fd: FileDesciptor? = null
for (permission in activity.contentResolver.persistedUriPermissions) {
if (permission.uri.path == "path/to/file") {
val docFile = DocumentFile.fromTreeUri(activity, permission.uri)!!.
createFile("video/mp4", "test.mp4")
fd = activity.contentResolver.
openFileDescriptor(docFile!!.uri, "w")!!.
fileDesciptor
break
}
}
val muxer = MediaMuxer(fd, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
但是文件描述符版本构造函数
public MediaMuxer(@NonNull FileDescriptor fd, @Format int format) throws IOException
如果API级别<26则不可用。
只有文件路径版本
public MediaMuxer(@NonNull String path, @Format int format) throws IOException
我可以使用。
但是在SAF的允许下,我无法获得真实的路径。
docFile.uri.path // this gives me a path with strange prefix.
因此,如果Android API级别低于26,我可以通过MediaMuxer在可移动SD卡中指定的任何文件夹中记录文件吗?
我和您有同样的问题,没有任何帮助。但是可能有一些解决方法。
您可以将文件记录到内部存储中,并且在记录结束时,可以使用SAF将记录文件复制到可移动SD中