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

如何在Xamarin.Android中离线加载SVF到Autodesk Forge Viewer?

Xamarin.Android本地加载SVF到Forge Viewer主视图不显示问题排查与解决

问题背景

在Xamarin.Android项目中本地加载SVF文件到Autodesk Forge Viewer,已将配套HTML资源放入项目Assets/html目录,但仅能显示侧边视图,主视图空白。核心代码如下:

MyWebViewClient.cs 拦截逻辑

public WebResourceResponse ShouldInterceptRequest(WebView webView, IWebResourceRequest request)
{
    try
    {
        Android.Net.Uri url = request.Url;
        String path = url.Path;
        if (path.StartsWith("/android_asset/"))
        {
            try
            {
                AssetManager assetManager = this.context.Assets;
                String relPath = path.Replace("/android_asset/", "").Replace("gz", "gz.mp3");
                return new WebResourceResponse(null, null, assetManager.Open(relPath));
            }
            catch (IOException ex)
            {
                String str = ex.Message;
            }
        }
    }
    catch (Exception ex) { }

    return null;
}

Activity.cs WebView初始化代码

SetContentView(Resource.Layout.webview);
var wbMain = FindViewById<WebView>(Resource.Id.webView1);
wbMain.Settings.DomStorageEnabled = true;
wbMain.Settings.JavaScriptEnabled = true;
wbMain.Settings.AllowFileAccessFromFileURLs = true;
wbMain.Settings.AllowUniversalAccessFromFileURLs = true;
var customWebViewClient = new MyWebViewClient(BaseContext);
customWebViewClient.OnPageLoaded += MyWebViewClient_OnPageLoaded;

wbMain.SetWebViewClient(customWebViewClient);
wbMain.LoadUrl("file:///android_asset/html/index.html");

主视图空白截图:
主视图空白截图

问题原因及解决方法

1. 资源路径错误替换

代码中relPath = path.Replace("/android_asset/", "").Replace("gz", "gz.mp3")会将所有带gz后缀的资源(比如Forge Viewer依赖的模型压缩切片文件)错误修改为.gz.mp3,导致模型资源无法找到,主视图无法渲染。

修复:移除错误的Replace("gz", "gz.mp3"),修正后代码:

String relPath = path.Replace("/android_asset/", "");

2. MIME类型未正确设置

创建WebResourceResponse时传入null作为MIME类型和编码,WebView无法正确识别资源类型,导致JS、CSS、模型资源加载异常。

修复:根据资源后缀设置对应MIME类型,示例代码:

string mimeType = null;
string encoding = "UTF-8";

if (path.EndsWith(".html")) mimeType = "text/html";
else if (path.EndsWith(".js")) mimeType = "application/javascript";
else if (path.EndsWith(".css")) mimeType = "text/css";
else if (path.EndsWith(".json")) mimeType = "application/json";
else if (path.EndsWith(".gz")) mimeType = "application/gzip";
else if (path.EndsWith(".png")) mimeType = "image/png";
else if (path.EndsWith(".jpg")) mimeType = "image/jpeg";
// 可根据实际资源类型扩展

return new WebResourceResponse(mimeType, encoding, assetManager.Open(relPath));

3. 验证SVF资源完整性

确保复制的SVF文件包完整,包含所有关联的.svf、.gz、.json等文件,且文件路径与HTML中加载逻辑一致。

4. 补充WebView配置

针对部分Android版本,需补充以下配置:

wbMain.Settings.LoadWithOverviewMode = true;
wbMain.Settings.UseWideViewPort = true;
wbMain.Settings.AllowFileAccess = true;

// Android 10+ 允许混合内容加载
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
    wbMain.Settings.MixedContentMode = Android.Webkit.MixedContentHandling.AlwaysAllow;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 20:50:25