简体   繁体   中英

mediaRecorder.prepare(); gives error Getting file not found exception when trying to create file

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.

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM