简体   繁体   English

使用谷歌文档在webview中显示pdf文件(存储在谷歌驱动器中)

[英]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 所以

  1. I've downloaded the pdf doc 我已经下载了pdf文档
  2. uploaded in google drive 上传到谷歌驱动器
  3. Got my own link for pdf 得到了我自己的pdf链接
  4. Trying to open that in webview using google docs 尝试使用谷歌文档在webview打开它
  5. FAILED 失败

Now the links are as follows 现在链接如下

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. 我跟随CommonsWareStuart SieglerSamir 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.

相关问题 将存储在谷歌驱动器中的 PDF 文件显示到 WebView - Display PDF file stored in google drive to WebView 使用Google Docs在WebView中打开本地PDF文件 - Open local PDF file in WebView using Google Docs 在WebView / Google文档中显示带有动态生成网址的pdf - Display a pdf with dynamic generating url in WebView / Google Docs 使用Google Docs在WebView中显示PDF不再起作用 - Displaying PDF in WebView using Google Docs is not working anymore 使用Google文档在Webview中隐藏pdf的打印/下载选项 - Hide Print / download options for pdf in Webview using Google docs Android WebView-使用Google文档打开PDF的重定向问题 - Android WebView - Redirect issue using Google Docs to open PDF 尝试使用 google drive 在 android 应用程序(使用 kotlin)webview 中显示在线 pdf 有时会产生错误 - Trying to display online pdf's in an android application (using kotlin) webview using google drive will occasionally produce errors 如何使用 WebView 从 Google Drive 下载 PDF 文件 - How To Download PDF File From Google Drive with WebView 有没有办法使用谷歌文档查看器显示javascript blob(.pdf)而不在本地保存文件? - Is there a way to display the javascript blob (.pdf) using google docs viewer without saving the file locally? 在Google Drive中显示pdf - display pdf within google drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM