在Android 11上,由于应用程序对存储访问权限的限制,可能会导致在运行时找不到FFmpeg二进制文件。为了解决这个问题,可以使用以下方法:
File ffmpegFile = new File(getFilesDir(), "ffmpeg");
if (!ffmpegFile.exists()) {
InputStream inputStream = getResources().openRawResource(R.raw.ffmpeg_binary);
FileOutputStream outputStream = new FileOutputStream(ffmpegFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
}
在上述代码中,R.raw.ffmpeg_binary
是放置FFmpeg二进制文件的res/raw
目录下的资源文件。
requestLegacyExternalStorage
属性,允许应用程序在Android 11上继续访问存储。在
标签中添加以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
上述代码将打开应用程序的设置页面,允许用户手动授予存储权限。
通过以上方法之一,你应该能够解决Android 11上找不到FFmpeg二进制文件的问题。