📜  如何在Android中从URL加载PDF?

📅  最后修改于: 2021-05-08 19:54:41             🧑  作者: Mango

大多数应用程序都需要包括支持才能在其应用程序中显示PDF文件。因此,如果我们必须在我们的应用程序中使用多个PDF文件,则几乎不可能在我们的应用程序中添加每个PDF文件,因为这种方法可能会导致应用程序大小增加,并且没有用户愿意使用此类文件下载应用程序巨大的尺寸。因此,要解决与尺寸有关的问题,我们将直接从服务器在我们的应用程序内部加载PDF文件,而无需实际将文件保存在我们的应用程序中。从服务器加载PDF文件将有助于我们管理应用程序大小的增加。因此,在本文中,我们将介绍如何从Android应用程序内的URL加载PDF文件。

PDFView的实现

为了添加此PDF视图,我们使用了一个库,该库将帮助我们从URL加载PDF。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

第2步:将依赖项添加到build.gradle(Module:app)

导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。

现在同步选项将出现在右上角,单击立即同步选项。

步骤3:在您的AndroidManifest.xml文件中向互联网添加权限

在您的AndroidManifest.xml文件中的以下两行中添加。

步骤4:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML


  
    
    
  


Java
import android.os.AsyncTask;
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.github.barteksc.pdfviewer.PDFView;
  
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
  
import javax.net.ssl.HttpsURLConnection;
  
public class MainActivity extends AppCompatActivity {
  
    // creating a variable
    // for PDF view.
    PDFView pdfView;
  
    // url of our PDF file.
    String pdfurl = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initializing our pdf view.
        pdfView = findViewById(R.id.idPDFView);
        new RetrivePDFfromUrl().execute(pdfurl);
    }
  
    // create an async task class for loading pdf file from URL.
    class RetrivePDFfromUrl extends AsyncTask {
        @Override
        protected InputStream doInBackground(String... strings) {
            // we are using inputstream 
            // for getting out PDF.
            InputStream inputStream = null;
            try {
                URL url = new URL(strings[0]);
                // below is the step where we are 
                // creating our connection.
                HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                    // response is success.
                    // we are getting input stream from url 
                    // and storing it in our variable.
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
                  
            } catch (IOException e) {
                // this is the method 
                // to handle errors.
                e.printStackTrace();
                return null;
            }
            return inputStream;
        }
  
        @Override
        protected void onPostExecute(InputStream inputStream) {
            // after the execution of our async 
            // task we are loading our pdf in our pdf view.
            pdfView.fromStream(inputStream).load();
        }
    }
}


步骤5:使用MainActivity。 Java文件

导航到应用程序> Java >您的应用程序包名称> MainActivity。 Java文件。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.AsyncTask;
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.github.barteksc.pdfviewer.PDFView;
  
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
  
import javax.net.ssl.HttpsURLConnection;
  
public class MainActivity extends AppCompatActivity {
  
    // creating a variable
    // for PDF view.
    PDFView pdfView;
  
    // url of our PDF file.
    String pdfurl = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initializing our pdf view.
        pdfView = findViewById(R.id.idPDFView);
        new RetrivePDFfromUrl().execute(pdfurl);
    }
  
    // create an async task class for loading pdf file from URL.
    class RetrivePDFfromUrl extends AsyncTask {
        @Override
        protected InputStream doInBackground(String... strings) {
            // we are using inputstream 
            // for getting out PDF.
            InputStream inputStream = null;
            try {
                URL url = new URL(strings[0]);
                // below is the step where we are 
                // creating our connection.
                HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                    // response is success.
                    // we are getting input stream from url 
                    // and storing it in our variable.
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
                  
            } catch (IOException e) {
                // this is the method 
                // to handle errors.
                e.printStackTrace();
                return null;
            }
            return inputStream;
        }
  
        @Override
        protected void onPostExecute(InputStream inputStream) {
            // after the execution of our async 
            // task we are loading our pdf in our pdf view.
            pdfView.fromStream(inputStream).load();
        }
    }
}

输出:

检出项目: https : //github.com/ChaitanyaMunje/GFGImageSlider/tree/PDFViewer

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!