1
try{
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

        videoUri = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+new StringBuilder("/EDMTRecord_").append(new SimpleDateFormat("dd-MM-yyyy-hh_mm_ss").format(new Date())).append(".mp4").toString();

        mediaRecorder.setOutputFile(videoUri);
        mediaRecorder.setVideoSize(DISPLAY_WIDTH,DISPLAY_HEIGHT);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncodingBitRate(512*1000);
        mediaRecorder.setVideoFrameRate(30);

        int rotation=getWindowManager().getDefaultDisplay().getRotation();
        int orientation=ORIENTATIONS.get(rotation+90);

        mediaRecorder.setOrientationHint(orientation);
        mediaRecorder.prepare();

    } catch (IOException e) {
        Log.e("ERROR",e.toString());
        e.printStackTrace();
    }

E/ERROR: java.io.FileNotFoundException: /storage/emulated/0/Download/EDMTRecord_22-06-2020-01_00_14.mp4: open failed: EACCES (Permission denied)

permissions are given

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unitedcoders.screenrecorder">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

As seen above the permissions are given in manifest and the permissions are been checked before this.

2 Answers2

2

This error usually occurs because the permission is not configured correctly in the manifest, Error shows that you have not permission to open the file.

<manifest>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        ...
        <application>
            ...
            <activity> 
                ...
            </activity>
        </application>
    </manifest> 

Check: https://stackoverflow.com/a/9907574

ARandomGuy
  • 21
  • 2
0

There are two main reasons check if you have run time WRITE_EXTERNAL_STORAGE

secondly put uri permission in manifest

android:grantUriPermissions="true"
android:requestLegacyExternalStorage="true"
Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33