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.

相关推荐
CodeCraft Studio3 小时前
【金融行业案例】基于Vaadin全栈Java框架重构内部系统,全面提升开发效率与用户体验
java·金融·重构·vaadin·银行内部系统·纯java开发框架·java web框架
潇I洒3 小时前
Linux写sh开机启动脚本-bash报错的两种解决方法
java·linux·bash
zhilin_tang3 小时前
揭开Linux跨平台 adb调试原理神秘面纱
android·linux
R.lin4 小时前
使用注解将日志存入Elasticsearch
java·大数据·后端·elasticsearch·中间件
滴滴滴嘟嘟嘟.4 小时前
全屏定时提醒工具
java·开发语言
せいしゅん青春之我4 小时前
【JavaEE初阶】网络原理——TCP处理先发后至问题
java·网络·笔记·网络协议·tcp/ip·java-ee
撩得Android一次心动4 小时前
Android 四大组件——Activity
android
星星落进兜里5 小时前
Spring全家桶面试题, 只补充细节版本
java·后端·spring
TimeFine5 小时前
Android 网络请求超时?可能与连接池和脏连接有关
android