Android模拟器
Android Studio 4.2.1
Android:9.0
在AndroidManifest.xml中,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.androidhello">
<application
android:requestLegacyExternalStorage="true"
...
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ScreenRecordService" />
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>我的自定义服务类ScreenRecordService:
...
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date curDate = new Date(System.currentTimeMillis());
String curTime = formatter.format(curDate).replace(" ", "");
String videoQuality = "HD";
if(isVideoSd) videoQuality = "SD";
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/" + videoQuality + curTime + ".mp4");
...应用程序运行失败,出现以下错误:
/storage/emulated/0/Movies/SD2021-06-01-08-26-55.mp4: W/System.err: java.io.FileNotFoundException: java.io.FileNotFoundException打开失败: EACCES (权限拒绝)
发布于 2021-06-02 06:34:54
在mediaRecorder.setOutputFile之前,询问运行时权限。
String[] permissions = { Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
ActivityCompat.requestPermissions(this, permissions, 123);https://stackoverflow.com/questions/67785567
复制相似问题