mediaRecorder.prepare(); 在尝试创建文件时出现错误"找不到文件"异常


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/错误:java.io.FileNotFound异常:/storage/emulated/0/Download/EDMTRecord_22-06-2020-01_00_14.mp4:打开失败:EACCES(权限被拒绝(

授予权限

<?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>

如上所示,权限在清单中给出,在此之前检查权限。

此错误通常是因为清单中未正确配置权限,错误显示您没有打开文件的权限。

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

检查:https://stackoverflow.com/a/9907574

主要有两个原因 检查您是否有运行时WRITE_EXTERNAL_STORAGE

其次,将 URI 权限放在清单中

android:grantUriPermissions="true"
android:requestLegacyExternalStorage="true"

相关内容

最新更新