You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor MAUI Hybrid Android无法显示应用数据目录视频——自定义文件提供器无效

Android端BlazorWebView加载应用私有目录文件的404问题解决

问题原因

Windows端文件系统访问无额外限制,而Android端WebView默认对私有目录文件的访问有约束,且PhysicalFileProvider在Android环境下可能无法正确解析应用私有目录的文件路径。

解决步骤

1. 开启Android WebView本地文件访问权限并替换FileProvider

在自定义的MyCustomWebview中添加平台特定代码,允许WebView访问本地文件,同时替换Android端的PhysicalFileProvider为自定义实现:

public class MyCustomWebview : BlazorWebView
{
    protected override void OnInitialized()
    {
        base.OnInitialized();
#if ANDROID
        var androidWebView = (Android.Webkit.WebView)Handler.PlatformView;
        androidWebView.Settings.AllowFileAccess = true;
        androidWebView.Settings.AllowFileAccessFromFileURLs = true;
#endif
    }

    public override IFileProvider CreateFileProvider(string contentRootDir)
    {
        var contentPath = Path.Combine(FileSystem.Current.AppDataDirectory, FolderConstants.ContentFolder);
        Directory.CreateDirectory(contentPath);
        
        IFileProvider customFileProvider;
#if ANDROID
        customFileProvider = new AndroidAppDataFileProvider(contentPath);
#else
        customFileProvider = new PhysicalFileProvider(contentPath);
#endif
        
        return new CompositeFileProvider(customFileProvider, base.CreateFileProvider(contentRootDir));
    }
}

// 适配Android应用私有目录的自定义文件提供者
public class AndroidAppDataFileProvider : IFileProvider
{
    private readonly string _rootPath;

    public AndroidAppDataFileProvider(string rootPath)
    {
        _rootPath = rootPath;
    }

    public IDirectoryContents GetDirectoryContents(string subpath)
    {
        var fullPath = Path.Combine(_rootPath, subpath.TrimStart('/'));
        if (!Directory.Exists(fullPath))
            return NotFoundDirectoryContents.Singleton;
        
        var files = Directory.EnumerateFiles(fullPath)
                            .Select(f => new PhysicalFileInfo(new FileInfo(f)));
        return new EnumerableDirectoryContents(files);
    }

    public IFileInfo GetFileInfo(string subpath)
    {
        var fullPath = Path.Combine(_rootPath, subpath.TrimStart('/'));
        if (!File.Exists(fullPath))
            return new NotFoundFileInfo(subpath);
        
        return new PhysicalFileInfo(new FileInfo(fullPath));
    }

    public IChangeToken Watch(string filter)
    {
        return NullChangeToken.Singleton;
    }
}

2. 验证路径一致性

确认FolderConstants.ContentFolder的值为"Content",确保文件实际路径/data/user/0/com.mypackage.whatever/files/Content/VideoContent/MyVideo.mp4,与自定义FileProvider根目录(.../files/Content)加上Blazor请求路径(/VideoContent/MyVideo.mp4)完全匹配。

3. 检查AndroidManifest.xml兼容性配置(可选)

若问题仍存在,确保AndroidManifest.xml未限制WebView的文件访问,可添加以下配置保障兼容性:

<application android:usesCleartextTraffic="true">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

内容的提问来源于stack exchange,提问作者rman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 08:35:09