Android 查看pdf文档——PDFView

 最近在搞Android显示word文档跟pdf文档的显示,分享一个显示pdf文档的小Demo(ps:word文档的显示网上挺多的)。

这里写图片描述 
这里使用的是PDFView,是GitHub上面的一个开源项目点击查看项目这个项目是一个Android Studio的项目,我用的是ecplice但是也不妨碍使用,只需要自己新建一个项目将pdfview中的相关内容进行拷贝,之后设置Demo项目的preference—Android——添加lib即可。(pdflib资源http://download.csdn.net/detail/danfengw/9679024) 
这里写图片描述

public class MainActivity extends Activity {
    private Button mbutton_scanpdf;
    private PDFView mPDFview;
    private static String urlpath = "http://www.ti.com.cn/cn/lit/ug/tidub07/tidub07.pdf";
    private String pdfNameAll="a.pdf";
    private String pdfName;
    private ProgressDialog dialog;
    private String outfilepath;
    private File outfile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbutton_scanpdf=(Button) findViewById(R.id.button_scanpdf);
        mPDFview=(PDFView) findViewById(R.id.pdfview);

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            outfilepath=Environment.getExternalStorageDirectory().getAbsolutePath();
        }   
        pdfNameAll=urlpath.substring(urlpath.lastIndexOf("/"));
        pdfName=pdfNameAll.substring(pdfNameAll.indexOf("."));

        mbutton_scanpdf.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 dialog=ProgressDialog.show(MainActivity.this, "", "正在加载");

                DownloadPDF downloadpdf=new DownloadPDF();
                downloadpdf.execute();

            }
        });

    }


   @Override
protected void onDestroy() {
       Timer timer=new Timer();
       timer.schedule(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(outfile.exists()){
                   outfile.delete();
                   Log.e("删除文件", ""+outfile.exists());
               }

        }
    }, 6000);
       timer.cancel();
        super.onDestroy();
}
    class DownloadPDF extends AsyncTask<String, String, String>{
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        URL url;
        try {
            url = new URL(urlpath);
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setDoInput(true);
            conn.setConnectTimeout(3000);
            conn.connect();
            if(HttpURLConnection.HTTP_OK==conn.getResponseCode()){
                byte[] bytes=new byte[1024];
                InputStream is=conn.getInputStream();
                 outfile=new File(outfilepath+"/",pdfNameAll);

                if(!outfile.exists()){
                    outfile.createNewFile();
                }
                FileOutputStream fos=new FileOutputStream(outfile);
                int len=-1;
                while((len=is.read(bytes))>0){
                    fos.write(bytes,0,len);
                }
                    fos.flush();
                    fos.close();
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{

        }
        return "下载完成";
    }
      @Override
    protected void onPostExecute(String result) {
        //  dialog.show();
          Log.e("result值", result);
          dialog.dismiss();
          mPDFview.fromFile(outfile)
            .defaultPage(1)
            .showMinimap(false)
            .enableSwipe(true)        
            .load();

        super.onPostExecute(result);
    } 
      @Override
    protected void onProgressUpdate(String... values) {
          super.onProgressUpdate(values);


    }

   }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120


PS: 
记得添加权限

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

注意PDFView能否展示你的pdf文档还取决于的pdf文档的编辑器,如果pdf文档编辑器的版本太高,pdfview仍然无法打开该pdf文档,因此建议采用mupdf或者调用第三方软件。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值