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.

相关推荐
虫小宝几秒前
导购APP高可用数据库设计:主从分离与分库分表在返利系统中的应用
android·数据库
独自破碎E9 分钟前
PO、VO、BO、DTO、DAO、POJO有什么区别?
java
czlczl2002092511 分钟前
从 SSO 登录到跨系统资源访问:OAuth2 全链路交互详解
java·spring boot·后端·spring·架构
茶本无香14 分钟前
单例模式深度解析:饿汉式与懒汉式的实现与选择
java·单例模式
爬山算法18 分钟前
Hibernate(29)什么是Hibernate的连接池?
java·后端·hibernate
Fuly102421 分钟前
软件研发类项目流程
java
我命由我1234522 分钟前
Android Jetpack Compose - TopAppBar、BottomAppBar、Scaffold
android·java·java-ee·kotlin·android studio·android jetpack·android-studio
我爱娃哈哈23 分钟前
SpringBoot + Aviator + 规则中心:轻量级表达式引擎实现营销优惠动态计算
java·spring boot·后端
廋到被风吹走23 分钟前
【Spring】IoC容器深度解析:Bean生命周期与循环依赖三级缓存
java·spring·缓存