How to open local PDF file in WebView in Android?

Viewed 59578

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

I already tried this:

webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setAllowFileAccess(true);
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");

final Uri uri = Uri.fromFile(file);

webview.loadUrl(uri.toString());

But it's still not opening it, so let me know how I can open a PDF in WebView?

8 Answers

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

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

WebView can easily be used for displaying PDF file from a web url but if you have local PDF file, then it becomes a pain.

In my case i first stored a reference of my local file:-

File file = new File(getExternalFilesDir(null),"your_pdf_file.pdf");

Then i obtained file path URI of my local PDF file using FileProvider and started an intent for opening it using existing applications that can open PDF documents:-

try{
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(FileProvider.getUriForFile(BaseActivity.this,BuildConfig.APPLICATION_ID +".provider",file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
    } catch (ActivityNotFoundException e) {
    Toast.makeText(BaseActivity.this,"No Application Available to View PDF",Toast.LENGTH_SHORT).show();
    }

Also to use FileProvider API, you need to declare it in manifest as:-

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

And declare file_paths.xml under XML resource folder as:-

<paths>
    <external-path name="external_files" path="."/>
</paths>
Related