我在模拟器中使用了 Android 10 AVD。我将示例视频文件(“sample.mp4”)复制到模拟器,然后尝试播放它。该 Activity 有一个 VideoView
和一个 Button
。我尝试在单击按钮时播放视频,但因“权限被拒绝”而失败。当然,我已经添加了“外部存储读取”权限,并在“应用信息”中手动启用了它。但由于它不起作用,我还添加了 requestPermissions()
并在运行时启用了权限。两者均无效。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
videoView.setVideoPath("/sdcard/Download/sample.mp4");
videoView.start();
}
val permissions = arrayOf<String>(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
requestPermissions(permissions, 2222)
错误信息:
W/VideoView: Unable to open content: /sdcard/Download/sample.mp4
java.io.FileNotFoundException: /sdcard/Download/sample.mp4: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:496)
不是因为路径不对,因为我给的是假路径,错误不一样。
Unable to open content: /sdcard/Download/samplexxx.mp4
java.io.FileNotFoundException: /sdcard/Download/samplexxx.mp4: open failed: ENOENT (No such file or directory)
传递 /storage/emulated/0/Download/sample.mp4
给了我同样的“权限被拒绝”错误。
最佳答案
尝试此代码获取访问权限
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
}
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
}ode here
关于android - VideoView无法打开下载目录中的视频文件(权限被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57902676/