package com.youche.cango;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import java.io.File;
public class MainActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener {
private PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = (PDFView)findViewById(R.id.pdfView);
//pdfView.fromFile(File)
//pdfView.fromBytes(byte[])
//pdfView.fromStream(InputStream)
//pdfView.fromSource(DocumentSource)
File file = new File(Environment.getExternalStorageDirectory() + "/CANGO/", "loan.pdf");
pdfView.fromFile(file)
.pages(0) // all pages are displayed by default
.enableSwipe(true)
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
//.onDraw(onDrawListener)
.onLoad(this)
.onPageChange(this)
//.onPageScroll(onPageScrollListener)
//.onError(onErrorListener)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.load();
}
@Override
public void loadComplete(int nbPages) {
}
@Override
public void onPageChanged(int page, int pageCount) {
}
}
2.布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.youche.cango.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
compile 'com.github.barteksc:android-pdf-viewer:2.4.0'
权限 :
<!--获取sd卡写的权限,用于文件上传和下载-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--允许读取手机状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- 在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />