简体   繁体   English

从Android Studio HTML文档在外部应用程序中打开本地PDF

[英]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. 当前正在制作WebView HTML应用程序,并且我指向本地PDF文件的链接(路径是相对的)不起作用。 I just want it to open in an external application (not within the WebView). 我只希望它在外部应用程序中打开(而不是在WebView中)。 I have tried multiple PDF intents from this site with no success. 我已尝试从此站点尝试多个PDF意图,但均未成功。

I originally had the Override above the public Boolean launchPDF but kept getting 我最初在public Boolean launchPDF之上具有Override ,但一直

: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. 我需要这是所有PDF的总括声明,因为我有很多。

AndroidManifest.xml: 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/ . 一种选择是使用我的StreamProvider能够直接从assets/从您的应用程序提供PDF文件。 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 . 您将在ACTION_VIEW Intent使用StreamProvider提供的Uri 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. 但是,这不适用于所有PDF查看器,尤其是不是Google Drive应用程序中的一部分,除非他们修复了它们的错误。

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. 如果需要更大的兼容性,请将资产复制到本地文件系统上的文件中(例如,在getCacheDir() ),然后使用Android支持库中的FileProvider Other than having to do the copying, this has the same basic effect as does using StreamProvider . 除了必须进行复制外,这与使用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. 通过文件而不是直接从APK内素材资源提供服务可以改善与PDF查看器的兼容性,尽管由于它们处理content Uri值的方式存在错误,仍有一些可能会出现问题。

Yet another option is to embed some sort of PDF-viewing capability into your app. 另一个选择是将某种PDF查看功能嵌入到您的应用程序中。 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. 它们都在兼容性和大小方面进行了权衡,没有一个功能比独立的PDF查看器更好。

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 . 或者,您可以找到一些工具将PDF转换为HTML / CSS / images,将其打包在您的应用中,然后在WebView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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