Android 如何本地加載pdf文件
大部分app打開pdf文件是通過intent調起手機中能打開pdf文件的工具,來查看pdf文件,如果需求是,用戶在app內下載好pdf文件后,不通過第三方的工具,本地打開。
這樣的需求要怎么實現呢?上網查了一些資料,發(fā)現了一個很好用PDF開源庫。
使用起來也很簡單,首先添加PDFView的引用
compile 'com.github.barteksc:android-pdf-viewer:2.4.0'
布局中引用PdfView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/common_title" /> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdf_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
接下來就是下載pdf文件,為了節(jié)省用戶資源,在每次下載之前檢查一下本地是否有該pdf文件,如果有直接打開,沒有的話再去下載。
這里我寫了一個加載中的對話框,打開過程中和下載過程中用的都是這一個
if (CheckFileExist(title)){ builderShow = new CustomDialog(ShowPDFActivity.this); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.dialog_pdf_progress_new, null); builderShow.setContentView(view); builderShow.show(); isDownload=false; refushUI(); }else { isDownload=true; DownLoadPDF.getInstance().downLoadPDF(ShowPDFActivity.this, //下載路徑); }
如果本地有pdf文件,則開始加載pdf文件,refushUI();
public void refushUI(){ try { pdfView.fromFile(new File(//pdf文件的絕對路徑,//標題)) .defaultPage(1) .enableAnnotationRendering(false) .onLoad(new OnLoadCompleteListener() { @Override public void loadComplete(int nbPages) { if (isDownload){ DownLoadPDF.getInstance().closeDilaoig(); } if (builderShow != null&&builderShow.isShowing()) { builderShow.dismiss(); } } }) .scrollHandle(null) .load(); }catch (Exception e){ e.printStackTrace(); } }
PDFView加載pdf文件有兩種形式,一種是從文件中讀取,還有一種就是從assets目錄中讀取
private void displayFromAssets(String assetFileName ) { pdfView.fromAsset(assetFileName) //設置pdf文件地址 .defaultPage(6) //設置默認顯示第1頁 .onPageChange(this) //設置翻頁監(jiān)聽 .onLoad(this) //設置加載監(jiān)聽 .onDraw(this) //繪圖監(jiān)聽 .showMinimap(false) //pdf放大的時候,是否在屏幕的右上角生成小地圖 .swipeVertical( false ) //pdf文檔翻頁是否是垂直翻頁,默認是左右滑動翻頁 .enableSwipe(true) //是否允許翻頁,默認是允許翻頁 // .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 過濾掉 .load(); } private void displayFromFile( File file ) { pdfView.fromFile(file) //設置pdf文件地址 .defaultPage(6) //設置默認顯示第1頁 .onPageChange(this) //設置翻頁監(jiān)聽 .onLoad(this) //設置加載監(jiān)聽 .onDraw(this) //繪圖監(jiān)聽 .showMinimap(false) //pdf放大的時候,是否在屏幕的右上角生成小地圖 .swipeVertical( false ) //pdf文檔翻頁是否是垂直翻頁,默認是左右滑動翻頁 .enableSwipe(true) //是否允許翻頁,默認是允許翻 // .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 過濾掉 .load(); }
本地沒有pdf文件,需要從服務端獲取,
DownLoadPDF.getInstance().downLoadPDF(ShowPDFActivity.this, //下載路徑);
public class DownLoadPDF { private static Context context; private static File file ; private static CustomDialog builder = null ; private static Handler ddhandle; private static DownLoadPDF instance = null; public static DownLoadPDF getInstance(){ if(instance==null){ synchronized (DownLoadPDF.class){ if(instance==null){ instance = new DownLoadPDF(); } } } return instance; } public void downLoadPDF(final Context con, final String url, final String title, final Handler ddhandler) { ddhandle = ddhandler; context = con; builder = new CustomDialog(con); LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.dialog_pdf_progress_new, null); builder.setContentView(view); builder.show(); new Thread() { @Override public void run() { try { file = getFileFromServer(url,title); sleep(200); if (file != null) { handler.sendEmptyMessage(2); } } catch (Exception e) { e.printStackTrace(); builder.dismiss(); handler.sendEmptyMessage(-1); } } }.start(); } public void closeDilaoig(){ if (builder != null&&builder.isShowing()) { builder.dismiss(); } }public static int length ; public static File getFileFromServer(String path,String title) throws Exception { // 如果相等的話表示當前的sdcard掛載在手機上并且是可用的 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setDoInput(true); conn.connect(); length = conn.getContentLength(); InputStream is = conn.getInputStream(); //將pdf文件存儲在指定文件夾下 File filePath = new File(//指定文件夾路徑); if (!filePath.exists()){ filePath.mkdir(); } File file = new File(filePath , title+".pdf"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1) { fos.write(buffer, 0, len); handler.sendEmptyMessage(0); } fos.close(); bis.close(); is.close(); return file; } else { handler.sendEmptyMessage(-1); return null; } } private static Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: break; case -1: //下載失敗 Toast.makeText(context, "下載失敗,請稍后再試!", Toast.LENGTH_SHORT).show(); break; case 2: ddhandle.sendEmptyMessage(100); break; default: break; } } }; }
大家可以看到,在pdf問價下載成功的時候handler.sendEmptyMessage(2);,當case為2的時候,通過調用該工具類的頁面?zhèn)鬟^來的ddhandle重新發(fā)送了一個消息,
調用界面收到消息后會重新調用refushUI();這個方法來打開pdf文件。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
Android基于Http協(xié)議實現文件上傳功能的方法
這篇文章主要介紹了Android基于Http協(xié)議實現文件上傳功能的方法,結合實例形式分析了Android的HTTP協(xié)議原理與文件上傳功能實現技巧,需要的朋友可以參考下2016-07-07Android布局技巧之創(chuàng)建可重用的UI組件
這篇文章主要為大家詳細介紹了Android布局技巧之創(chuàng)建可重用的UI組件,文中提到了include標簽的使用方法,感興趣的小伙伴們可以參考一下2016-05-05Android中使用AsyncTask實現文件下載以及進度更新提示
AsyncTask,它使創(chuàng)建需要與用戶界面交互的長時間運行的任務變得更簡單,本篇文章主要介紹了Android中使用AsyncTask實現文件下載以及進度更新提示,有興趣的可以了解一下。2016-12-12