Android中VideoView播放S3预签名URL视频提示无内容提供者错误
Android VideoView播放S3预签名URL报错的解决方法
问题原因
VideoView的setVideoURI(Uri)方法无法正确解析带有URL编码特殊字符(如签名中的%2B、%3D)的S3预签名HTTPS URL,默认Content Provider不支持这类包含复杂参数的网络地址,因此抛出FileNotFoundException。
解决方法
方法1:用setVideoPath替代setVideoURI
先对预签名URL进行解码,再传入setVideoPath:
import java.net.URLDecoder; import java.nio.charset.StandardCharsets; // 解码URL中的编码字符 String decodedUrl = URLDecoder.decode(Video_url, StandardCharsets.UTF_8.name()); videoView.setVideoPath(decodedUrl); videoView.start();
方法2:自定义MediaDataSource加载视频
如果方法1无效,可通过MediaDataSource手动处理网络请求,灵活性更高:
videoView.setMediaController(new MediaController(this)); new Thread(() -> { try { URL url = new URL(Video_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); InputStream inputStream = conn.getInputStream(); MediaDataSource dataSource = new MediaDataSource() { @Override public long getSize() { return conn.getContentLength(); } @Override public int readAt(long position, byte[] buffer, int offset, int size) throws IOException { inputStream.skip(position - inputStream.available()); return inputStream.read(buffer, offset, size); } @Override public void close() throws IOException { inputStream.close(); conn.disconnect(); } }; runOnUiThread(() -> { videoView.setVideoDataSource(dataSource); videoView.prepare(); videoView.start(); }); } catch (IOException e) { e.printStackTrace(); } }).start();
额外检查项
- 确保App已申请网络权限,在
AndroidManifest.xml中添加:
<uses-permission android:name="android.permission.INTERNET" />
- 先复制预签名URL到浏览器测试,确认视频可正常播放,排除URL过期或权限配置错误的问题。
内容的提问来源于stack exchange,提问作者Rakesh
相关产品推荐
相关产品推荐

