Android使用mupdf查看pdf文档

 

操作流程:

 先将pdf下载下来,然后再使用mupdf查看。

例子中包含了编译的mupdf的so文件,和调用的java文件,另外自定义了一个可以多点触控实现缩放查看的控件,另外我添加了下标。

  1.下载pdf文件->调用mupdf控件查看

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.artifex.mupdfdemo.MuPDFCore;
import com.artifex.mupdfdemo.MuPDFPageAdapter;
import com.unipus.ilearning.R;
import com.unipus.ilearning.ui.base.BaseFragment;
import com.unipus.ilearning.ui.widget.PdfReaderViewWithIndex;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * 电子书阅读页面( 百度)
 *
 * @author likoha
 * @created 2017/03/14 14:35:04
 */
@SuppressLint("NewApi")
public class ReadingBDFragmentNew extends BaseFragment {
    private String url;
    private PdfReaderViewWithIndex pdfReaderView;
    private LinearLayout ll_container;
    private MuPDFCore mCore;
    private MuPDFPageAdapter mAdapter;
    private String mPdfFilePath;
    private String resourceName;
    private int contentLen;

    public static final String BUNDLE_KEY_TITLE = "detail_title";
    public static final String BUNDLE_KEY_URL = "detail_url";
    public static final String BUNDLE_KEY_ID = "res_id";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_bd_read, container, false);
        ll_container = (LinearLayout) rootView.findViewById(R.id.ll_container);

        resourceName = getArguments().getString(BUNDLE_KEY_TITLE);
        url = getArguments().getString(BUNDLE_KEY_URL);
        mPdfFilePath = getActivity().getExternalCacheDir().getPath() + File.separator + resourceName + ".pdf";

        initData();
        return rootView;
    }

    @Override
    public void initData() {
        super.initData();

        if (new File(mPdfFilePath).exists()) {
            openPdf();
        } else {
            downloadPdf();
        }

    }

    private void downloadPdf() {
        new DownLoad().execute(url);
    }

    private void openPdf() {
        ll_container.removeAllViews();
        pdfReaderView = new PdfReaderViewWithIndex(getActivity());
        try {
            mCore = new MuPDFCore(getActivity(), mPdfFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mCore != null && mCore.countPages() == 0) {
            mCore = null;
        }
        if (null == mCore) {
            Toast.makeText(getActivity(), "文件已损坏,无法打开", Toast.LENGTH_SHORT).show();
            return;
        }
        mAdapter = new MuPDFPageAdapter(getActivity(), mCore);
        if (pdfReaderView.getReaderVeiw() != null) {
            pdfReaderView.getReaderVeiw().setAdapter(mAdapter);
            //真正解析pdf的是MuPDFCore,只有它知道pdf有多少页。
            pdfReaderView.setCount(mCore.countPages());
            ll_container.addView(pdfReaderView);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    class DownLoad extends AsyncTask<String, Integer, String> {

        private static final int PROGRESS_MAX = 0X001;
        private static final int UPDATE = 0X002;

        /**
         * Trust every server - dont check for any certificate
         */
        private void trustAllHosts() {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[]{};
                }

                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//                    Log.i("skyapp", "checkClientTrusted");
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//                    Log.i("skyapp", "checkServerTrusted");
                }
            }};
            // Install the all-trusting trust manager
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showLoading();
        }

        //在onPreExecute执行,该方法运行在后台线程中
        @Override
        protected String doInBackground(String... params) {
            try {
                URL url = new URL(params[0]);

                trustAllHosts();

                //获取连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                //获取下载文件的大小
                contentLen = connection.getContentLength();
                //根据下载文件大小设置进度条最大值(使用标记区别实时进度更新)
                publishProgress(PROGRESS_MAX, contentLen);
                //循环下载(边读取边存入)
                BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(mPdfFilePath));
                int len = -1;
                byte[] bytes = new byte[1024];
                while ((len = bis.read(bytes)) != -1) {
                    bos.write(bytes, 0, len);
                    bos.flush();
                    //实时更新下载进度(使用标记区别最大值)
                    publishProgress(UPDATE, len);
                }
                bos.close();
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
                return "0";
            }
            return "1";
        }

        //在publishProgress被调用后,UI thread会调用这个方法
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            switch (values[0]) {
                case PROGRESS_MAX:
//                    progress.setMax(values[1]);
                    break;
                case UPDATE:
//                    progress.incrementProgressBy(values[1]);
//                    //获取下载进度百分比并更新textview
//                    int i=(progress.getProgress()*100)/contentLen;
//                    tv.setText("下载进度为:"+i+"%");
                    break;
            }
        }

        //doInBackground方法执行完后被UI thread执行
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            hideLoading();

            if ("0".equals(s)) {
                Toast.makeText(getActivity(), "下载失败", Toast.LENGTH_SHORT).show();
            } else {
                openPdf();
            }
        }

    }

}

2.自定义的View,封装了一下下标

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.artifex.mupdfdemo.ReaderView;
import com.unipus.ilearning.R;

public class PdfReaderViewWithIndex extends RelativeLayout {

    private ReaderView mReaderVeiw;
    private TextView mIndex;
    private int count = 0;

    public PdfReaderViewWithIndex(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.pdf_reader_view_with_index, this);

        mReaderVeiw = (ReaderView) findViewById(R.id.readerView);
        mIndex = (TextView) findViewById(R.id.tv_index);


        mReaderVeiw.setOnPageChangeListener(new ReaderView.OnPageChangeListener() {
            @Override
            public void onMoved(int index) {
                mIndex.setText((index + 1) + "/" + count);
            }
        });
    }

    public PdfReaderViewWithIndex(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PdfReaderViewWithIndex(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ReaderView getReaderVeiw() {
        if (mReaderVeiw != null) {
            return mReaderVeiw;
        }
        return null;
    }

    public void setCount(int count) {
        this.count = count;
        mIndex.setText("1/" + count);
    }

}

3. pdf_reader_view_with_index.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.artifex.mupdfdemo.ReaderView
        android:id="@+id/readerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tv_index"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:paddingBottom="20dip"
        android:text=""
        android:textSize="14sp" />

</RelativeLayout>

其他重要的文件如下,包含几个java类,和编译好的so文件,把它们加到项目中就可以使用了。需要注意的是so文件编译的时候

里面已经写死了调用java类的路径,所以java类的包名目录结构不能改。如下:

java类和so的下载地址:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值