如何使用 PDFViewer 在 Android 的 Webview 中打开 pdf

问题描述

我一直在尝试创建一个 Android 应用程序,该应用程序将在 WebView 中打开一个网站。例如,我想打开 Google.com 并导航到一个 pdf 文件。我希望能够在应用程序中打开 pdf,而不是下载并在本地打开。

我已经在我的代码中实现了 PDFViewer,因此最终我将能够在应用程序中的 PDFViewer 中打开 pdf,然后在我完成使用后退按钮后导航回网站,而无需离开应用程序.

在activity_main.xml文件中,我在Webview下面对PDFViewer进行了编码,这样它在调用的时候就可以打开pdf,但是现在我只得到一个空白页。如果我能得到任何帮助,我将不胜感激!

下面是我的 MainActivity.java 文件


import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import com.github.barteksc.pdfviewer.PDFView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private WebView mywebView;
    PDFView pdfView;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) this.findViewById(R.id.webView);
        WebSettings webSettings= mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebView.loadUrl("www.google.com");

                if (url.endsWith(".pdf")) {
                    PDFView pdfView = findViewById(R.id.pdfView);
                    new RetrivePdffromUrl().execute(url);

                } else {
                    mywebView.loadUrl(url);
                }
    }


    class RetrivePdffromUrl extends AsyncTask<String,Void,InputStream> {
        @Override
        protected InputStream doInBackground(String... strings) {
         
            InputStream inputStream = null;
            try {
                URL url = new URL(strings[0]);
                
                HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                   
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }

            } catch (IOException e) {
               
                e.printstacktrace();
                return null;
            }
            return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream).load();
        }
    }

    @Override
    public void onBackpressed() {
        if(mywebView.canGoBack()){
            mywebView.goBack();
        }else{
            super.onBackpressed();
        }

    }
}

这是我的activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </WebView>
</RelativeLayout>

我对 Android 开发还很陌生,所以如果我能得到任何帮助,我将不胜感激。提前致谢!!!

解决方法

我认为他们是一个小问题 String url;

之后你直接使用了,没有在if语句中赋值

if(url.endsWith(".pdf"))

如果我错了,我很抱歉。