【发布时间】:2020-05-04 00:36:17
【问题描述】:
我有 local html file 我想将 html 保存到本地路径中加载的 Webview。
但第一次总是保存空数据。
try
{
htmltopdfs = html;
var dir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/SVSTATIONERY/");
var file = new Java.IO.File(dir + "/" + fileName + ".pdf");
if (!dir.Exists())
dir.Mkdirs();
//else
// dir.Delete();
int x = 0;
while (file.Exists())
{
x++;
file = new Java.IO.File(dir + "/" + fileName + "( " + x + " )" + ".pdf");
}
//if (webpage == null)
var webpage = new Android.Webkit.WebView(MainActivity.context);
int width = 2102;
int height = 2970;
webpage.Layout(0, 0, width, height);
webpage.LoadDataWithBaseURL("", htmltopdfs, "text/html", "UTF-8", null);
webpage.SetWebViewClient(new WebViewCallBack(file.ToString()));
return file.ToString();
}
catch (Java.Lang.Exception e)
{
return e.Message;
}
Onpagefinished event
class WebViewCallBack : WebViewClient
{
string fileNameWithPath = null;
public WebViewCallBack(string path)
{
this.fileNameWithPath = path;
}
public override void OnPageFinished(Android.Webkit.WebView myWebview, string url)
{
PdfDocument document = new Android.Graphics.Pdf.PdfDocument();
PdfDocument.Page page = document.StartPage(new PdfDocument.PageInfo.Builder(2102, 3500, 1).Create());
myWebview.Draw(page.Canvas);
document.FinishPage(page);
Stream filestream = new MemoryStream();
FileOutputStream fos = new Java.IO.FileOutputStream(fileNameWithPath, false);
document.WriteTo(filestream);
fos.Write(((MemoryStream)filestream).ToArray(), 0, (int)filestream.Length);
fos.Close();
}
}
创建后,但第一次,空白页面已创建到本地路径,然后创建多次后正常工作。
【问题讨论】:
标签: android xamarin mobile xamarin.forms webview