Android 如何本地加載pdf文件
大部分app打開pdf文件是通過(guò)intent調(diào)起手機(jī)中能打開pdf文件的工具,來(lái)查看pdf文件,如果需求是,用戶在app內(nèi)下載好pdf文件后,不通過(guò)第三方的工具,本地打開。
這樣的需求要怎么實(shí)現(xiàn)呢?上網(wǎng)查了一些資料,發(fā)現(xiàn)了一個(gè)很好用PDF開源庫(kù)。
使用起來(lái)也很簡(jiǎn)單,首先添加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>
接下來(lái)就是下載pdf文件,為了節(jié)省用戶資源,在每次下載之前檢查一下本地是否有該pdf文件,如果有直接打開,沒(méi)有的話再去下載。
這里我寫了一個(gè)加載中的對(duì)話框,打開過(guò)程中和下載過(guò)程中用的都是這一個(gè)
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文件的絕對(duì)路徑,//標(biāo)題))
.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) //設(shè)置pdf文件地址
.defaultPage(6) //設(shè)置默認(rèn)顯示第1頁(yè)
.onPageChange(this) //設(shè)置翻頁(yè)監(jiān)聽
.onLoad(this) //設(shè)置加載監(jiān)聽
.onDraw(this) //繪圖監(jiān)聽
.showMinimap(false) //pdf放大的時(shí)候,是否在屏幕的右上角生成小地圖
.swipeVertical( false ) //pdf文檔翻頁(yè)是否是垂直翻頁(yè),默認(rèn)是左右滑動(dòng)翻頁(yè)
.enableSwipe(true) //是否允許翻頁(yè),默認(rèn)是允許翻頁(yè)
// .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 過(guò)濾掉
.load();
}
private void displayFromFile( File file ) {
pdfView.fromFile(file) //設(shè)置pdf文件地址
.defaultPage(6) //設(shè)置默認(rèn)顯示第1頁(yè)
.onPageChange(this) //設(shè)置翻頁(yè)監(jiān)聽
.onLoad(this) //設(shè)置加載監(jiān)聽
.onDraw(this) //繪圖監(jiān)聽
.showMinimap(false) //pdf放大的時(shí)候,是否在屏幕的右上角生成小地圖
.swipeVertical( false ) //pdf文檔翻頁(yè)是否是垂直翻頁(yè),默認(rèn)是左右滑動(dòng)翻頁(yè)
.enableSwipe(true) //是否允許翻頁(yè),默認(rèn)是允許翻
// .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 過(guò)濾掉
.load();
}
本地沒(méi)有pdf文件,需要從服務(wù)端獲取,
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 {
// 如果相等的話表示當(dāng)前的sdcard掛載在手機(jī)上并且是可用的
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文件存儲(chǔ)在指定文件夾下
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, "下載失敗,請(qǐng)稍后再試!", Toast.LENGTH_SHORT).show();
break;
case 2:
ddhandle.sendEmptyMessage(100);
break;
default:
break;
}
}
};
}
大家可以看到,在pdf問(wèn)價(jià)下載成功的時(shí)候handler.sendEmptyMessage(2);,當(dāng)case為2的時(shí)候,通過(guò)調(diào)用該工具類的頁(yè)面?zhèn)鬟^(guò)來(lái)的ddhandle重新發(fā)送了一個(gè)消息,
調(diào)用界面收到消息后會(huì)重新調(diào)用refushUI();這個(gè)方法來(lái)打開pdf文件。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android startActivityForResult實(shí)例詳解
- Android在Fragment中實(shí)現(xiàn)監(jiān)聽觸摸事件
- Android 使用FragmentTabhost代替Tabhost
- Android數(shù)據(jù)持久化之File機(jī)制分析
- Android數(shù)據(jù)持久化之Preferences機(jī)制詳解
- Android利用FlexboxLayout輕松實(shí)現(xiàn)流動(dòng)布局
- android手機(jī)端與PC端使用adb forword通信
- Android 中SwipeRefreshLayout與ViewPager滑動(dòng)事件沖突解決方法
- Android 7.0行為變更 FileUriExposedException解決方法
相關(guān)文章
Android7.0中關(guān)于ContentProvider組件詳解
本文描述了Android7.0中關(guān)于ContentProvider組件實(shí)現(xiàn)原理以及ContentProvider發(fā)布者和調(diào)用者這兩在Framework層是如何實(shí)現(xiàn)的。2017-11-11
Android項(xiàng)目遷移到AndroidX的方法步驟
這篇文章主要介紹了Android項(xiàng)目遷移到AndroidX的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
解析ScrollView--仿QQ空間標(biāo)題欄漸變
本篇文章主要介紹了仿QQ空間標(biāo)題欄漸變的實(shí)現(xiàn)方法的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05
Windows下搭建Android開發(fā)環(huán)境
這篇文章主要介紹了Windows下搭建Android開發(fā)環(huán)境,需要的朋友可以參考下2015-09-09
Android?動(dòng)態(tài)加載?so實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Android?動(dòng)態(tài)加載?so實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android答題APP的設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android答題APP的設(shè)計(jì)與實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android 拍照選擇圖片并上傳功能的實(shí)現(xiàn)思路(包含權(quán)限動(dòng)態(tài)獲取)
這篇文章主要介紹了Android 拍照(選擇圖片)并上傳(包含權(quán)限動(dòng)態(tài)獲取),本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Android連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)方法,在Android應(yīng)用程序中連接MySQL數(shù)據(jù)庫(kù)可以幫助開發(fā)人員實(shí)現(xiàn)更豐富的數(shù)據(jù)管理功能,而且在Android中操作數(shù)據(jù)庫(kù)真的太智能了,需要的朋友可以參考下2024-02-02
Android中recyclerView底部添加透明漸變效果
這篇文章主要給大家介紹了關(guān)于Android中recyclerView如何實(shí)現(xiàn)底部添加透明漸變效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2018-04-04

