转https://www.jb51.net/article/136364.htm

Android的WebView做不到ios的WebView那样可以很方便的直接预览pdf文件。要实现利用WebView预览pdf我们可以使用谷歌文档服务:

mWebView.loadUrl("http://docs.google.com/gviewembedded=true&url=" + pdfUrl);

这种方式国内网络环境是不用考虑的。当然也有替代的方案:我们可以使用mozilla开源的PDF.js。

Github

mozilla 官方demo

一 WebView设置:

1

2

3

4

5

WebSettings webSettings = mWebView.getSettings();

webSettings.setJavaScriptEnabled(true);

webSettings.setAllowFileAccess(true);

webSettings.setAllowFileAccessFromFileURLs(true);

webSettings.setAllowUniversalAccessFromFileURLs(true);

二 实现方式

方式一: 使用mozilla部署在github pages上的Viewer

1

View.loadUrl("http://mozilla.github.io/pdf.js/web/viewer.html?file=" + pdfUrl);

这种方式和使用google docs是差不多一样的,重要的是国内可以直接访问,但是会遇到跨域的问题。

方式二: 下载PDF.js放到assets目录下

如果pdf文件不能跨域访问的话可以使用这种方式,先把文件下载到本地然后传入本地文件路径预览pdf:

1

mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUrl);

PDF.js本身是一个比较大的库,如果全部放到本地的话apk差不多会增大5m左右。所以我们可以考虑吧PDF.js部署到服务端或者使用cdn的方式。

先下载,然后再打开.原文:https://blog.csdn.net/ming_147/article/details/53408233

public class PdfHttpDownloader {
    private Context context;

public PdfHttpDownloader(Context context) {
        this.context = context;
    }

/**
     * 打开pdf
     * pdf:PDF url
     * name:pdf文件的名字
     */
    public void startPdfActivity(String pdf, String name) {
        String terPath = getSDPath() + "/trader/" + name + ".PDF";
        File file = new File(terPath);
        if (file.exists()) {
            Intent intent = getPdfFileIntent(terPath);
            context.startActivity(intent);
        } else {
            downLoadPdf(pdf, name);
        }
    }

public Intent getPdfFileIntent(String path) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(new File(path));
        i.setDataAndType(uri, "application/pdf");
        return i;
    }

public void downLoadPdf(final String pdf, final String name) {
        Request request = new Request.Builder()
                .url(pdf)
                .get()
                .build();
        OkHttpClient http = new OkHttpClient();
        http.newCall(request)
                .enqueue(new com.squareup.okhttp.Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                    }

@Override
                    public void onResponse(Response response) {
                        InputStream is = null;
                        byte[] buf = new byte[1024];
                        int len = 0;
                        FileOutputStream fos = null;
                        String terPath = null;
                        File file = null;
                        try {
                            is = response.body().byteStream();
                            terPath = getSDPath() + "/trader/" + name + ".PDF";
                            file = new File(terPath);
                            fos = new FileOutputStream(file);
                            while ((len = is.read(buf)) != -1) {
                                fos.write(buf, 0, len);
                            }
                            fos.flush();
                            //下载成功
                            if (file.exists()) {
                                Intent intent = getPdfFileIntent(terPath);
                                context.startActivity(intent);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (is != null)
                                    is.close();
                            } catch (IOException e) {
                            }
                            try {
                                if (fos != null)
                                    fos.close();
                            } catch (IOException e) {
                            }
                        }
                    }
                });
    }

private String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED);   //判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
        return sdDir.toString();
    }
}

Android打开pdf文件相关推荐

  1. android存储pdf文件怎么打开,android打开pdf文件

    我们在工作中肯定有需要,加载pdf或者doc的地方,但是,android没有提供一个好的打开方法,我又想吐槽下,人家ios可以直接打开的.. 有2钟方法打开pdf.doc. 方法一: 利用Intent ...

  2. Android用PdfRenderer类开发打开pdf文件的功能

    PdfRenderer是Android官方用于开发打开pdf文件功能的类,今天介绍一下它的最基本的使用.Android官方有一个相关的Sample,项目名是PdfRendererBasic,大家也可以 ...

  3. Android下载并打开PDF文件

    1.下载PDF文件到本地 private void downFile(){String urlString = "http://14.215.72.79/file3.data.weipan. ...

  4. android本地xml文件怎么打开,android 打开本地文件

    首先要知道的是,Android 打开本地文件是根据类型打开的,也就是根据文件的 MIME 类型来确定 如果不知道是什么类型,那就是 : */* 类型匹配表: private static final ...

  5. Android实现打开本地文件,Android 打开本地文件(示例代码)

    Android 打开本地的文件,目前来说,其实很常见.而且现在有手机版的office了.查看office的全家桶就更加方便. 首先要知道的是,Android 打开本地文件是根据类型打开的,也就是根据文 ...

  6. 使用第三方应用打开pdf文件

    /*** android获取一个用于打开PDF文件的intent** @param path 要打开的文件的绝对路径* @return*/public Intent getPdfFileIntent( ...

  7. android 打开部分文件方法汇总整理

    总结打开文件的的方法代码: /*** 打开文件* @param file*/ private void openFile(File file){ Intent intent = new Intent( ...

  8. android 打开本地文件

    这个是别人的代码,自己忘了地址,只是给自己记录插个眼 获取本地文件 在MainActivity 中写方法 /*** android 打开本地文件* @param path 本地文件路径(带文件名)*/ ...

  9. uniapp通过url或base64打开pdf文件

    1.通过url打开pdf文件: //通过url打开pdf文件openPdfFileByUrl(pdfUrl: string) {uni.showLoading({title: "下载中,请稍 ...

  10. 打开PDF文件弹出阅读未加标签文档的解决方法

    打开PDF文件弹出阅读未加标签文档的解决方法 参考文章: (1)打开PDF文件弹出阅读未加标签文档的解决方法 (2)https://www.cnblogs.com/Tty725/p/3308065.h ...

最新文章

  1. 一个基于特征向量的近似网页去重算法——term用SVM人工提取训练,基于term的特征向量,倒排索引查询相似文档,同时利用cos计算相似度...
  2. 主要技术指标简介_期货常用技术指标(五)布林线
  3. Django之创建应用以及配置路由
  4. Participate in E-sports【Java大数+二分】
  5. ruby学习--block
  6. python新手入门代码-新手必看:手把手教你入门 Python
  7. log4cplus c++开源日志系统
  8. C++ ../ ./的区别
  9. Atitit lucence 使用总结 目录 1. 基本概念 1 1.1.   Index:索引库,文档的集合组成索引。 1 2. 建立索引 2 2.1. Api查询 2 2.2. Dsl查询 3
  10. 计算机安装xp蓝屏怎么办,联想笔记本装xp系统蓝屏解决方法
  11. 编写广告系统的测试用例
  12. 设置名字的第一个字为默认头像
  13. 腹有诗书气自华,新华三交换机大有不同
  14. Google Gmail Oauth Client ID 认证指南
  15. HarvestText
  16. Python也能识别图文,看到好的文章就用它一键扫描吧
  17. 【Android实现返回主页,禁止返回上一层等功能】
  18. PacBio三代宏基因组测序大幅提升海洋水体宏基因组研究效果
  19. 网易云linux版本如何安装包,网易云音乐linux版下载-网易云音乐linux 安装包v1.1.0 官方版 - 极光下载站...
  20. pycharm pep8 代码格式化

热门文章

  1. bcnf分解算法_BCNF的保持无损连接的分解
  2. [论文写作-词汇] 这么多特别,该用哪个?special especial specific particular peculiar exceptional extraordinary
  3. 小程序的转发分享功能
  4. ps切图怎么做成html,PS切图怎么导出网页 PS切图怎么生成源代码
  5. 小白重装系统步骤总结
  6. Linux重启 Redis自动启动
  7. 阿里云主要产品及功能介绍,阿里云产品分为6大分类:云计算基础/安全/大数据/人工智能/企业应用/物联网
  8. android锁屏了怎么办,安卓手机被恶意锁屏该怎么办
  9. 关于卸载office的问题:office无法卸载的办法(附office安装和注册表查看)
  10. word里怎么在右上角添加标注[1]