正常情况
1、Setting
- settings.gradle
groovy
dependencyResolutionManagement {
...
repositories {
...
maven { url 'https://jitpack.io' }
maven { url "https://repository.liferay.com/nexus/content/repositories/public/" }
}
}
- 模块级 build.gradle
groovy
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
2、Activity Layout
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".viewpdf.ViewPDFActivity">
<Button
android:id="@+id/btn_open_pdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开 PDF 文件"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfv_container"
android:layout_width="300dp"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_open_pdf" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、Activity Code
java
Button btnOpenPdf = findViewById(R.id.btn_open_pdf);
PDFView pdfvContainer = findViewById(R.id.pdfv_container);
btnOpenPdf.setOnClickListener(v -> {
File pdfFile = new File(getFilesDir(), "test.pdf");
InputStream inputStream;
try {
inputStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "PDF 文件不存在", Toast.LENGTH_SHORT).show();
return;
}
pdfvContainer.fromStream(inputStream)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.load();
});
一、数据为 null
1、基本测试
java
private byte[] getData() {
return null;
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.load();
-
底层报错,应用闪退,输出结果如下
D Init FPDF library
java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: java_array == null
java_vm_ext.cc:570] in call to GetPrimitiveArray
java_vm_ext.cc:570] from long com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(byte[], java.lang.String)
runtime.cc:647] Runtime aborting...
runtime.cc:647] Dumping all threads without mutator lock held
2、异常捕获测试
java
private byte[] getData() {
return null;
}
java
byte[] data = getData();
try {
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.load();
} catch (Exception e) {
e.printStackTrace();
}
-
异常捕获无效,底层报错,应用闪退,输出结果如下
D Init FPDF library
java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: java_array == null
java_vm_ext.cc:570] in call to GetPrimitiveArray
java_vm_ext.cc:570] from long com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(byte[], java.lang.String)
runtime.cc:647] Runtime aborting...
runtime.cc:647] Dumping all threads without mutator lock held
3、onError 回调测试
java
private byte[] getData() {
return null;
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.onError(t -> {
Log.i(TAG, "加载 PDF 失败:" + t.getMessage());
})
.load();
-
onError 回调无效,底层报错,应用闪退,输出结果如下
D Init FPDF library
java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: java_array == null
java_vm_ext.cc:570] in call to GetPrimitiveArray
java_vm_ext.cc:570] from long com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(byte[], java.lang.String)
runtime.cc:647] Runtime aborting...
runtime.cc:647] Dumping all threads without mutator lock held
二、数据为空
1、基本测试
java
private byte[] getData() {
return new byte[0];
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.load();
-
报错,应用不闪退,异常在库内被捕获,输出结果如下
load pdf error
java.io.IOException: cannot create document: File not in PDF format or corrupted.
at com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(Native Method)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:150)
at com.github.barteksc.pdfviewer.source.ByteArraySource.createDocument(ByteArraySource.java:35)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:27)...
2、onError 回调测试
java
private byte[] getData() {
return new byte[0];
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.onError(t -> {
Log.i(TAG, "加载 PDF 失败:" + t.getMessage());
})
.load();
-
onError 回调触发,输出结果如下
加载 PDF 失败:cannot create document: File not in PDF format or corrupted.
三、数据异常(PDF 文件损坏)
1、基本测试
java
private byte[] getData() {
try {
File pdfFile = new File(getFilesDir(), "test.pdf");
InputStream inputStream = new FileInputStream(pdfFile);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
byte[] bytes = buffer.toByteArray();
// 替换 100 个字节
for (int i = 0; i < 100; i++) {
bytes[i] = (byte) (bytes[i] + 1);
}
return bytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.load();
-
报错,应用不闪退,异常在库内被捕获,输出结果如下
load pdf error
java.io.IOException: cannot create document: File not in PDF format or corrupted.
at com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(Native Method)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:150)
at com.github.barteksc.pdfviewer.source.ByteArraySource.createDocument(ByteArraySource.java:35)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:27)...
2、onError 回调测试
java
private byte[] getData() {
try {
File pdfFile = new File(getFilesDir(), "test.pdf");
InputStream inputStream = new FileInputStream(pdfFile);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
byte[] bytes = buffer.toByteArray();
// 替换 100 个字节
for (int i = 0; i < 100; i++) {
bytes[i] = (byte) (bytes[i] + 1);
}
return bytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.onError(t -> {
Log.i(TAG, "加载 PDF 失败:" + t.getMessage());
})
.load();
-
onError 回调触发,输出结果如下
加载 PDF 失败:cannot create document: File not in PDF format or corrupted.
四、数据异常(非 PDF 文件)
1、基本测试
java
private byte[] getData() {
try {
File pdfFile = new File(getFilesDir(), "test.docx");
InputStream inputStream = new FileInputStream(pdfFile);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.load();
-
报错,应用不闪退,异常在库内被捕获,输出结果如下
load pdf error
java.io.IOException: cannot create document: File not in PDF format or corrupted.
at com.shockwave.pdfium.PdfiumCore.nativeOpenMemDocument(Native Method)
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:150)
at com.github.barteksc.pdfviewer.source.ByteArraySource.createDocument(ByteArraySource.java:35)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:27)...
2、onError 回调测试
java
private byte[] getData() {
try {
File pdfFile = new File(getFilesDir(), "test.docx");
InputStream inputStream = new FileInputStream(pdfFile);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java
byte[] data = getData();
pdfvContainer.fromBytes(data)
.enableSwipe(true) // 启用手势滑动翻页
.swipeHorizontal(false) // 设置滑动方向为垂直滑动
.enableDoubletap(true) // 启用双击缩放
.defaultPage(0) // 设置初始显示的页面
.onError(t -> {
Log.i(TAG, "加载 PDF 失败:" + t.getMessage());
})
.load();
-
onError 回调触发,输出结果如下
加载 PDF 失败:cannot create document: File not in PDF format or corrupted.