2018April03_Android网络加载PDF文件

本文介绍了一种在Android应用中加载网络PDF文件的方法,探讨了使用webView加载、直接跳转浏览器、下载后调用本地APP及利用第三方PDFView库等方案,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#1 RequireMent
加载网络pdf文件: http://file.chmsp.com.cn/colligate/file/00100000224821.pdf

#2 scheme
(1) webView加载 ,谷歌服务有限制
(2) 跳转浏览器,体验差
(3) 下载后调用手机内APP,体验差
(4) 三方依赖库

#3 Core Code
(1) webView加载

	//找不到网页
private void webViewLoadPdf() {
        //wv
        WebView wb = findViewById(R.id.wv);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setPluginState(WebSettings.PluginState.ON);
        wb.loadUrl("https://docs.google.com/viewer?url=http://file.chmsp.com.cn/colligate/file/00100000224821.pdf");
    }

(2)跳转浏览器

	 //体验差 跳转浏览器,两次跳转(浏览器+ 点击下载后的通知跳转能够打开pdf的app)
    private void skipBrowser(){
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://file.chmsp.com.cn/colligate/file/00100000224821.pdf");
        intent.setData(content_url);
        startActivity(intent);
    }

(3)下载后调用app

	private void downLoadAndSkip(){
        new AppFileDownUtils("http://file.chmsp.com.cn/colligate/file/00100000224821.pdf", "00100000224821.pdf", new DownLoadListener() {
            @Override
            public void onDownLoadSuccess(File file) {
                startActivity(getPdfFileIntent(file));
            }

            @Override
            public void onDownLoadFailed(String msg) {

            }

            @Override
            public void onDownLoading(int progress) {
                Log.i("onDownLoading",">>> progress ="+progress);
            }
        }).start();
    }

    public Intent getPdfFileIntent(File file) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        return Intent.createChooser(intent, "Open File");
    }
	

(4)三方依赖库:PDFView

    implementation 'com.github.barteksc:android-pdf-viewer:2.7.0-beta.1'
		
			 pdfView.fromFile(file)
//                .pages(0, 2, 3, 4, 5); // 把0 , 2 , 3 , 4 , 5 过滤掉
                //是否允许翻页,默认是允许翻页
                .enableSwipe(true)
                //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
                .swipeHorizontal(true)
                //
                .enableDoubletap(false)
                //设置默认显示第0页
                .defaultPage(0)
                //允许在当前页面上绘制一些内容,通常在屏幕中间可见。
//                .onDraw(onDrawListener)
//                // 允许在每一页上单独绘制一个页面。只调用可见页面
//                .onDrawAll(onDrawListener)
                //设置加载监听
                .onLoad(new OnLoadCompleteListener() {
                    @Override
                    public void loadComplete(int nbPages) {
                        pageTv.setText(nbPages + "");
                        pageTv1.setText(0 +  "/");
                    }
                })
                //设置翻页监听
                .onPageChange(new OnPageChangeListener() {

                    @Override
                    public void onPageChanged(int page, int pageCount) {
                        p = page;
                        pageTv1.setText(page + "/");
                    }
                })
                //设置页面滑动监听
//                .onPageScroll(onPageScrollListener)
//                .onError(onErrorListener)
                // 首次提交文档后调用。
//                .onRender(onRenderListener)
                // 渲染风格(就像注释,颜色或表单)
                .enableAnnotationRendering(false)
                .password(null)
                .scrollHandle(null)
                // 改善低分辨率屏幕上的渲染
                .enableAntialiasing(true)
                // 页面间的间距。定义间距颜色,设置背景视图
                .spacing(0)
                .load();
		

#4 Demo地址
Android网络加载PDF文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值