简体   繁体   中英

Open Local PDF in external Application from Android Studio HTML document

Currently making a WebView HTML app, and my links to local PDF files (paths are relative), are not working. I just want it to open in an external application (not within the WebView). I have tried multiple PDF intents from this site with no success.

I originally had the Override above the public Boolean launchPDF but kept getting

:method does not override or implement  method from a supertype

I need this to be a blanket statement for all PDFs as I have many of them.

AndroidManifest.xml:

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        myWebView.loadUrl("file:///android_asset/index.html");
    }

    public boolean launchPDF(WebView view, String url) {
        if (urlIsPDF(url)) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "application/pdf");
            try {
                view.getContext().startActivity(intent);
            } catch (ActivityNotFoundException e) {
                //User does not have pdf viewer installed
            }
        } else {
            myWebView.loadUrl(url);
        }
        return true;
    }

    public boolean urlIsPDF(String url) {
        return url.endsWith(".pdf");
    }
};

One option is to use my StreamProvider to be able to serve PDF files from your app, directly from assets/ . This is not too tough to set up and does not require you to copy any data at runtime. You would use a StreamProvider -supplied Uri in your ACTION_VIEW Intent . However, this does not work with all PDF viewers, notably not the one that is part of the Google Drive app, until they fix their bugs.

If you want greater compatibility, copy the assets to files on the local filesystem (eg, in getCacheDir() ), then use FileProvider from the Android Support Library. Other than having to do the copying, this has the same basic effect as does using StreamProvider . Serving from files, rather than directly from in-APK assets, improves compatibility with PDF viewers, though a few may still have hiccups due to bugs in how they handle content Uri values.

Yet another option is to embed some sort of PDF-viewing capability into your app. In this blog post , I outline three options. They all have tradeoffs in terms of compatibility and size, and none are as full-featured as a standalone PDF viewer.

Or, you could find some tool to convert your PDFs to HTML/CSS/images, package that stuff in your app, and show that stuff in the WebView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM