Android PDF 操作 - AndroidPdfViewer 显示 PDF 异常清单(数据为 null、数据为空、PDF 文件损坏、非 PDF 文件)

正常情况

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.

相关推荐
SamDeepThinking3 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
雨白3 小时前
指针与数组的核心机制
android
她的男孩6 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
荣码7 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
黄林晴8 小时前
Room 3.0 正式发布!包名彻底重构,KMP 成为核心主线
android·android jetpack
三少爷的鞋9 小时前
Kotlin 协程环境下的 DCL 懒加载:别把线程时代的经验直接搬过来
android
plainGeekDev9 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波18 小时前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing