在 Xamarin.Froms 中创建 WebView 的扩展以显示 gif.我无法在 Android 上加载图像 [英] Creating an extension of WebView in Xamarin.Froms to display gifs. I cannot load the image on Android

查看:17
本文介绍了在 Xamarin.Froms 中创建 WebView 的扩展以显示 gif.我无法在 Android 上加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 按预期工作.我也尝试将 .gif 添加到资产文件夹(将 BaseUrl 设置为file:///android_asset"),但没有成功.有什么想法吗?

iOS works as expected. I have tried adding the .gif to the assets folder too (setting the BaseUrl to "file:///android_asset") with no luck. Any ideas?

imageHtml = "<center><body bgcolor =""
                        + GetBackgroundColorHex()
                        + """
                        + heightAndWidth
                        + ">"
                        + (!string.IsNullOrWhiteSpace(imageFileName)
                            ? "<img src="" 
                            + imageFileName 
                            + """
                            + heightAndWidth
                            + "/>"
                            : "&nbsp;")
                        + "</body></center>";

            HtmlWebViewSource source = new HtmlWebViewSource()
            {
                Html = imageHtml
            };

            if (Device.RuntimePlatform == Device.Android && !string.IsNullOrWhiteSpace(imageFileName))
            {
                source.BaseUrl = "file:///android_res/drawable/";
            }

            Source = source;

这是我在输出中得到的错误:

Here's the error I get in output:

12-11 02:08:00.256 E/AndroidProtocolHandler(12895): Unable to open resource URL: file:///android_res/drawable/Loading.gif
12-11 02:08:00.256 E/AndroidProtocolHandler(12895): java.lang.NoSuchFieldException: Loading
12-11 02:08:00.256 E/AndroidProtocolHandler(12895):     at java.lang.Class.getField(Class.java:1549)
12-11 02:08:00.256 E/AndroidProtocolHandler(12895):     at org.chromium.android_webview.AndroidProtocolHandler.getFieldId(AndroidProtocolHandler.java:40)
12-11 02:08:00.256 E/AndroidProtocolHandler(12895):     at org.chromium.android_webview.AndroidProtocolHandler.openResource(AndroidProtocolHandler.java:54)
12-11 02:08:00.256 E/AndroidProtocolHandler(12895):     at org.chromium.android_webview.AndroidProtocolHandler.open(AndroidProtocolHandler.java:10)

推荐答案

您可以通过资产加载 Android.Webkit.WebView 内容,而不是 drawable.

You can load Android.Webkit.WebView content via assets, not drawables.

以下是 Xamarin.Android 代码,用于解析图像以获取其宽度/高度以确保保留纵横比并从 Asset 文件夹中显示它:

Here is the Xamarin.Android code to parse the image to get its width/height to insure the aspect ratio is preserved and display it from the Asset folder:

var src = "out.gif";
var backgroundColor = "#ff0000";
int imageWidth;
int imageHeight;
using (var stream = Assets.Open(src))
using (var options = new BitmapFactory.Options { InJustDecodeBounds = true })
{
    await BitmapFactory.DecodeStreamAsync(stream, null, options);
    imageWidth = options.OutWidth;
    imageHeight = options.OutHeight;
}
var html = $"<body bgcolor={backgroundColor};"><img src="{src}" alt="A Gif file" width="{imageWidth}" height="{imageHeight}" style="width: 100%; height: auto;"/></body>";
webView.Settings.AllowFileAccessFromFileURLs = true;
webView.LoadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", "");

var src = "image.gif";
var backgroundColor = "#ff0000";
int imageWidth = 300;
int imageHeight = 200;
var html = $"<body bgcolor={backgroundColor};"><img src="{src}" alt="A Gif file" width="{imageWidth}" height="{imageHeight}" style="width: 100%; height: auto;"/></body>";
webView.Source = new HtmlWebViewSource
{
    Html = html
};

注意:image.gif 位于 Android Assets (AndroidResource) 文件夹和 iOS Resource 文件夹 (BundleResource) 中

Note: image.gif is in the Android Assets (AndroidResource) folder and iOS Resource folder (BundleResource)

这篇关于在 Xamarin.Froms 中创建 WebView 的扩展以显示 gif.我无法在 Android 上加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆