How to open local PDF file in WebView in Android?

前端 未结 7 950
终归单人心
终归单人心 2020-11-28 12:24

I want to open local (SD card) PDF file in a WebView.

I already tried this:

webview = (WebView) findViewById(R.id.webview);
webview.getSettings().set         


        
相关标签:
7条回答
  • 2020-11-28 12:31

    From android OS 5.0(lollipop) on-wards you can use PdfRenderer instead of webview/library. You can use this class to show pdf's within the app.

    If you want to support OS lower than that you can use a library/other approach mentioned in other answer as there is no native support.

    Read more about it from the docs, you can also refer this example

    0 讨论(0)
  • 2020-11-28 12:31

    you can use the SkewPdfView library for loading pdf from remote or local urls.

    First of all, Add following in your root build.gradle:

    allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
    

    }

    and add the SkewPdfView Library as:

    dependencies {
        implementation 'com.github.naya-aastra:SkewPdfView:1.1'
    }
    

    Now Include SkewPdfView in your layout:

    <com.nayaastra.skewpdfview.SkewPdfView
        android:id="@+id/skewPdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    Save all the local files in assets folder and then use SkewPdfView as follows. Load a PDF file programmatically as:

    SkewPdfView skewPdfView;
    skewPdfView = findViewById(R.id.skewPdfView);
    String pdfLink = "LINK TO ASSET FILE";
    skewPdfView.loadPdf(pdfLink);
    

    P.S. link of SkewPdfView library

    SkewPdfView Github Page

    0 讨论(0)
  • 2020-11-28 12:32

    As @Sameer replied in your comment above, the only solution to view PDF in webview is through Google Docs' online viewer which will render and send back a readable version to your app.

    Previously discussed here

    • Open PDF in a WebView
    • Open a pdf file inside a webview.
    0 讨论(0)
  • 2020-11-28 12:41

    After going through several posts I came across this simple answer on Quora which pretty much do the work. Following are steps:-

    Add this dependency in your gradle file:

    compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
    

    activity_main.xml

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

    MainActivity.java

    package pdfviewer.pdfviewer;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import com.github.barteksc.pdfviewer.PDFView;
    import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
    import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
    import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
    import com.shockwave.pdfium.PdfDocument;
    
    import java.util.List;
    
    public class MainActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener{
        private static final String TAG = MainActivity.class.getSimpleName();
        public static final String SAMPLE_FILE = "sample_pdf.pdf";
        PDFView pdfView;
        Integer pageNumber = 0;
        String pdfFileName;
    
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            pdfView= (PDFView)findViewById(R.id.pdfView);
            displayFromAsset(SAMPLE_FILE);
        }
    
        private void displayFromAsset(String assetFileName) {
            pdfFileName = assetFileName;
    
            pdfView.fromAsset(SAMPLE_FILE)
                    .defaultPage(pageNumber)
                    .enableSwipe(true)
    
                    .swipeHorizontal(false)
                    .onPageChange(this)
                    .enableAnnotationRendering(true)
                    .onLoad(this)
                    .scrollHandle(new DefaultScrollHandle(this))
                    .load();
        }
    
    
        @Override    public void onPageChanged(int page, int pageCount) {
            pageNumber = page;
            setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
        }
    
    
        @Override    public void loadComplete(int nbPages) {
            PdfDocument.Meta meta = pdfView.getDocumentMeta();
            printBookmarksTree(pdfView.getTableOfContents(), "-");
    
        }
    
        public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
            for (PdfDocument.Bookmark b : tree) {
    
                Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
    
                if (b.hasChildren()) {
                    printBookmarksTree(b.getChildren(), sep + "-");
                }
            }
        }
    
    }
    

    You have to make sure that your asset folder contains sample_pdf.pdf (Library also support opening pdf from Uri and SDCard)

    Happy coding :)

    0 讨论(0)
  • 2020-11-28 12:43

    WebView can not open a .pdf file. The most popular solution (google docs' url + your url in WebView) just shows you converted by google docs pictures. However there is still no simple way to open .pdf from url.

    0 讨论(0)
  • 2020-11-28 12:45

    You cannot. Using an Intent, you can open the PDF in an external viewer application like Acrobat Reader:

    try
    {
     Intent intentUrl = new Intent(Intent.ACTION_VIEW);
     intentUrl.setDataAndType(uri, "application/pdf");
     intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     mActivity.startActivity(intentUrl);
    }
    catch (ActivityNotFoundException e)
    {
     Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
    }
    
    0 讨论(0)
提交回复
热议问题