Reputation: 1312
Android Emulator
Android Studio 4.2.1
Android:9.0
in 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>
my customized Service class 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");
...
app runs failed with below errors:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Movies/SD2021-06-01-08-26-55.mp4: open failed: EACCES (Permission denied)
Upvotes: 0
Views: 630
Reputation: 1312
before mediaRecorder.setOutputFile,ask runtime permissions.
String[] permissions = { Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
ActivityCompat.requestPermissions(this, permissions, 123);
Upvotes: 1