【发布时间】:2018-07-23 13:08:42
【问题描述】:
在我的应用程序中,我实现了一个 webview 来显示指向该 webview 的网站链接。现在该网站有一个包含 pdf 文件链接的按钮。如果我点击网站上的那个按钮,它会在网上显示一个 pdf 文件。但是,如果我尝试在我的应用程序中将其打开到 webview 中,则什么也不会发生。我是 Xamarin android 的新手。我找不到任何合适的方法。这是我将网站显示到 webview 中的代码。
单击网站上的链接时,我想重新加载 pdf。但是结果和以前一样
修改后的代码
namespace Xamarin.PDFView
{
[Activity (Label = "PDFView", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private string _documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
private string _pdfPath;
private string _pdfFileName = "thePDFDocument.pdf";
private string _pdfFilePath;
private WebView _webView;
private string _webUrl = "https://Link of thewebsite";
private string _pdfURL = "https://Link of thewebsite/25052016.pdf";
private WebClient _webClient = new WebClient();
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
_webView = FindViewById<WebView> (Resource.Id.webView1);
var settings = _webView.Settings;
settings.JavaScriptEnabled = true;
settings.AllowFileAccessFromFileURLs = true;
settings.AllowUniversalAccessFromFileURLs = true;
settings.BuiltInZoomControls = true;
_webView.SetWebViewClient(new WebViewClient());
_webView.SetWebChromeClient(new WebChromeClient());
_webView.LoadUrl(_webUrl);
}
public override bool OnTouchEvent(MotionEvent e)
{
if (_webUrl.Contains(".pdf"))
{
DownloadPDFDocument();
}
return base.OnTouchEvent(e);
}
protected override void OnResume ()
{
base.OnResume ();
_webView.LoadUrl( "javascript:window.location.reload( true )" );
}
protected override void OnPause ()
{
base.OnPause ();
_webView.ClearCache(true);
}
private void DownloadPDFDocument()
{
AndHUD.Shared.Show(this, "Downloading PDF\nPlease Wait ..", -1, MaskType.Clear);
_pdfPath = _documentsPath + "/PDFView";
_pdfFilePath = Path.Combine(_pdfPath, _pdfFileName);
// Check if the PDFDirectory Exists
if(!Directory.Exists(_pdfPath)){
Directory.CreateDirectory(_pdfPath);
}
else{
// Check if the pdf is there, If Yes Delete It. Because we will download the fresh one just in a moment
if (File.Exists(_pdfFilePath)){
File.Delete(_pdfFilePath);
}
}
// This will be executed when the pdf download is completed
_webClient.DownloadDataCompleted += OnPDFDownloadCompleted;
// Lets downlaod the PDF Document
var url = new Uri(_pdfURL);
_webClient.DownloadDataAsync(url);
}
private void OnPDFDownloadCompleted (object sender, DownloadDataCompletedEventArgs e)
{
// Okay the download's done, Lets now save the data and reload the webview.
var pdfBytes = e.Result;
File.WriteAllBytes (_pdfFilePath, pdfBytes);
if(File.Exists(_pdfFilePath))
{
var bytes = File.ReadAllBytes(_pdfFilePath);
}
_webView.LoadUrl("file:///android_asset/pdfviewer/index.html?file=" + _pdfFilePath);
AndHUD.Shared.Dismiss();
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
}
}
【问题讨论】:
-
WebView 无法显示 pdf 文件。
-
@greenapps 有没有办法从链接中显示 pdf 文件
-
飞赞已经告诉过你了。
-
@greenapps 我已经修改了看到 github 的代码。现在我想首先显示网址。然后在 url 中存在另一个 pdf 的 url。我想表明当用户单击该按钮时。我在 Touch 方法上实现了一个布尔值。如果我点击 pdf 的链接,没有任何反应
标签: c# android pdf webview xamarin.android