问题描述
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
+ "/>"
: " ")
+ "</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
内容.
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资产(AndroidResource
)文件夹和iOS资源文件夹(BundleResource
)
Note: image.gif
is in the Android Assets (AndroidResource
) folder and iOS Resource folder (BundleResource
)
这篇关于在Xamarin.Froms中创建WebView扩展以显示gif.我无法在Android上加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!