[英]Display the pdf file(stored in google drive) in webview by using google docs
My pdf file is stored in some website ex: http://www.pdf995.com/samples/pdf.pdf 我的pdf文件存储在一些网站上: http : //www.pdf995.com/samples/pdf.pdf
Now I can't rely on other websites for my app. 现在我不能依赖其他网站的应用程序。 So
所以
webview
using google docs webview
打开它 Now the links are as follows 现在链接如下
pdf stored in other website link: " http://www.pdf995.com/samples/pdf.pdf " pdf存储在其他网站链接:“ http://www.pdf995.com/samples/pdf.pdf ”
Sharable link of pdf saved in google drive: " https://drive.google.com/open?id=0B8e4zX5Y1S0XV0U0UTZJOUVma00 " 保存在谷歌驱动器中的pdf的可共享链接:“ https://drive.google.com/open?id=0B8e4zX5Y1S0XV0U0UTZJOUVma00 ”
If it's preceded with google docs : " https://docs.google.com/gview?embedded=true&url=https://drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view " 如果之前是谷歌文档:“ https://docs.google.com/gview?embedded=true&url=https://drive.google.com/file/d/0B8e4zX5Y1S0XV0U0UTZJOUVma00/view ”
Result, 结果,
Other websites may change their url at anytime. 其他网站可能随时更改其网址。 Can't rely on it.
不能依靠它。
If I use the google drive link alone, then it is opening in web browser not within the app. 如果我单独使用谷歌驱动器链接,那么它将在不在应用程序内的Web浏览器中打开。
If preceded with google docs, it is not resulting in a pdf doc. 如果之前是谷歌文档,则不会产生pdf文档。 I get something like this.
我得到这样的东西。
What should be done so that I can use my own pdf from google drive to open in webview 应该做什么,以便我可以使用谷歌驱动器中的自己的pdf在webview中打开
I've followed CommonsWare , Stuart Siegler , Samir Mangroliya But nothing works. 我跟随CommonsWare , Stuart Siegler , Samir Mangroliya但没有任何作用。 :(
:(
you can view any pdf on the internet using google docs even without downloading it to the device. 您可以使用谷歌文档在互联网上查看任何pdf,甚至无需将其下载到设备。 Here is the sample of how to do it:
以下是如何操作的示例:
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
Where pdf
is a string link to your PDF file. 其中
pdf
是指向PDF文件的字符串链接。 So in your case it will be: 所以在你的情况下它将是:
String pdf = "http://www.pdf995.com/samples/pdf.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
I have a WebView, and the link to my pdf stored in google drive is: 我有一个WebView,存储在google驱动器中的我的pdf链接是:
String myPdfUrl = "https://drive.google.com/file/d/1fQfqgEmz-AiCpdHEIh7SNwdnAkQFqDQp/view";
Don't forget "/view". 别忘了“/ view”。
And my java code for display: 我的java代码显示:
private void displayWebView() {
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(myPdfUrl);
return true;
}
});
webView.loadUrl(myPdfUrl);
}
And add @SuppressLint("SetJavaScriptEnabled")
above onCreate()
并在
onCreate()
上面添加@SuppressLint("SetJavaScriptEnabled")
onCreate()
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act
Define Url like: 定义网址如:
String BASE_URL = "https://drive.google.com/viewerng/viewer?embedded=true&url=https://drive.google.com/uc?id=";
String PDF_ID = "<Your pdf id from Google drive share link>";
Load in WebView
: 在
WebView
加载:
webview.loadUrl(BASE_URL + PDF_ID);
First, you need to follow the answer provided by "Phúc Nghi Lâm." 首先,你需要按照“PhúcNghiLâm”提供的答案。 (the 3rd answer.)
(第3个答案。)
If it is not working.... 如果它不工作....
In my situation, there is HTML-result coming. 在我的情况下,会有HTML结果出现。 But your WebView's size is 0-width and 0-height.
但是你的WebView的大小是0宽和0高。
So you need to reset the LayoutParam by implement the "onPageFinished." 因此,您需要通过实现“onPageFinished”来重置LayoutParam。 Or something else.
或者是其他东西。
(You should be able to get the parent-view of the WebView. So.. 1.Get the size of parent-view. 2.Set the size data into a new LayoutParam. 3. Set the new LayoutParam to the WebView.) (您应该能够获得WebView的父视图。所以.. 1.获取父视图的大小.2。将大小数据设置为新的LayoutParam.3。将新的LayoutParam设置为WebView。)
I was suffering from same problem and after a lot of hit and trials, I found a way out. 我遇到了同样的问题,经过大量的打击和试验,我找到了出路。
You need to click on download button of google drive. 您需要点击谷歌硬盘的下载按钮。
as soon as you click it, a new tab will open with a different url, of this type 单击它后,将打开一个新选项卡,其中包含此类型的其他URL
https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download //test url// https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download //测试网址//
String url="https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download";
webView = (WebView) findViewById(R.id.progr);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.