在 Android 网络视图中通过 Google 驱动器显示 pdf 时显示空白页
Show blank page in showing pdf via Googler drive in Android webview
我在 WebView 中通过 https://docs.google.com/viewer?embedded=true&url={my pdf link} 显示我的 pdf。有时我的 WebView 在 "onReceivedError" 方法中显示一个空白页面而没有任何错误。为什么显示此空白页?
以此为例
class PdfViewActivity : AppCompatActivity() {
private lateinit var activityPdfViewBinding: ActivityPdfViewBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityPdfViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_pdf_view)
val path = "https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf" // Add Your URL here
loadPdfFromURL(path)
}
@SuppressLint("SetJavaScriptEnabled")
private fun loadPdfFromURL(path: String?) {
activityPdfViewBinding.webview.settings.loadWithOverviewMode = true
activityPdfViewBinding.webview.settings.javaScriptEnabled = true
val url = "https://docs.google.com/gview?embedded=true&url=$path"
activityPdfViewBinding.webview.loadUrl(url)
}
}
另外,将这些行添加到 onPageFinished
方法中
if (view.getTitle().equals("")) {
view.reload();
}
解决空白 gview 问题。您可以使用 HTML & JQuery.
HTML Code:
<iframe id="assetDivIdMob" src="https://docs.google.com/gview?embedded=true&url=YOUR-PDF-URL.pdf" width="100%" height="100%" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
JQuery code:
//Wait for some time & then call loadPDF
setTimeout(function(){
loadPDF();
},1050);
function loadPDF() {
//This statement will raise DOMException if gview PDF already loaded in Iframe & Code excution will stop.
document.getElementById('assetDivIdMob').contentWindow.document;
//If iframe is blank then reload Iframe.
$('#assetDivIdMob').attr('src', $('#assetDivIdMob').attr('src'));
console.log("reloading iframe...");
//Call loadPDF() after sometime & load PDF in Iframe till it is loaded in Iframe.
setTimeout(loadPDF, 2000);
}
我在 WebView 中通过 https://docs.google.com/viewer?embedded=true&url={my pdf link} 显示我的 pdf。有时我的 WebView 在 "onReceivedError" 方法中显示一个空白页面而没有任何错误。为什么显示此空白页?
以此为例
class PdfViewActivity : AppCompatActivity() {
private lateinit var activityPdfViewBinding: ActivityPdfViewBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityPdfViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_pdf_view)
val path = "https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf" // Add Your URL here
loadPdfFromURL(path)
}
@SuppressLint("SetJavaScriptEnabled")
private fun loadPdfFromURL(path: String?) {
activityPdfViewBinding.webview.settings.loadWithOverviewMode = true
activityPdfViewBinding.webview.settings.javaScriptEnabled = true
val url = "https://docs.google.com/gview?embedded=true&url=$path"
activityPdfViewBinding.webview.loadUrl(url)
}
}
另外,将这些行添加到 onPageFinished
方法中
if (view.getTitle().equals("")) {
view.reload();
}
解决空白 gview 问题。您可以使用 HTML & JQuery.
HTML Code:
<iframe id="assetDivIdMob" src="https://docs.google.com/gview?embedded=true&url=YOUR-PDF-URL.pdf" width="100%" height="100%" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
JQuery code:
//Wait for some time & then call loadPDF
setTimeout(function(){
loadPDF();
},1050);
function loadPDF() {
//This statement will raise DOMException if gview PDF already loaded in Iframe & Code excution will stop.
document.getElementById('assetDivIdMob').contentWindow.document;
//If iframe is blank then reload Iframe.
$('#assetDivIdMob').attr('src', $('#assetDivIdMob').attr('src'));
console.log("reloading iframe...");
//Call loadPDF() after sometime & load PDF in Iframe till it is loaded in Iframe.
setTimeout(loadPDF, 2000);
}