当前位置: 首页 > news >正文

Android 中 显示 PDF 文件内容(AndroidPdfViewer 库)

PDFView 是一个用于在 Android 应用中显示 PDF 文档的库。它提供了丰富的功能和灵活的配置选项,使得开发者能够轻松地在应用中嵌入 PDF 阅读器。

一、 添加依赖

  • 模块的 build.gradle 文件中添加以下依赖:
	// pdfimplementation("com.github.barteksc:android-pdf-viewer:3.2.0-beta.1")

二、添加 阿里云 Maven 仓库

  • 此时编译时会报错,提示无法找到该库
* What went wrong:
Execution failed for task ':app:mergeDebugNativeLibs'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.> Could not find com.github.barteksc:android-pdf-viewer:3.2.0-beta.1.Searched in the following locations:- https://dl.google.com/dl/android/maven2/com/github/barteksc/android-pdf-viewer/3.2.0-beta.1/android-pdf-viewer-3.2.0-beta.1.pom- https://repo.maven.apache.org/maven2/com/github/barteksc/android-pdf-viewer/3.2.0-beta.1/android-pdf-viewer-3.2.0-beta.1.pomRequired by:project :app
  • 在项目的 settings.gradle.kts 文件中增加 阿里云 Maven 仓库 支持。
dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {google()mavenCentral()// 阿里云 Maven 仓库maven { url = uri("https://maven.aliyun.com/repository/google") }maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }maven { url = uri("https://maven.aliyun.com/repository/public") }maven { url = uri("https://maven.aliyun.com/repository/central") }}
}

三、解决库冲突问题

  • 下载 android-pdf-viewer 依赖库后,编译提示 “com.android.support” 库冲突:
* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable> Duplicate class android.support.v4.app.AppLaunchChecker found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.FrameMetricsAggregator found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.FrameMetricsAggregator$FrameMetricsApi24Impl found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.FrameMetricsAggregator$FrameMetricsApi24Impl$1 found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.FrameMetricsAggregator$FrameMetricsBaseImpl found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.FrameMetricsAggregator$MetricType found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.13.1.aar -> core-1.13.1-runtime (androidx.core:core:1.13.1) and support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.13.1.aar -> core-1.13.1-runtime (androidx.core:core:1.13.1) and support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.13.1.aar -> core-1.13.1-runtime (androidx.core:core:1.13.1) and support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)Duplicate class android.support.v4.app.NavUtils found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)Duplicate class android.support.v4.app.TaskStackBuilder found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)......Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)

错误分析

  • 这个错误是由于新增的依赖库中包含了Android Support Library 的依赖,跟项目中使用的 AndroidX 发生了类冲突。从 Android 9.0(API 级别 28)开始,Android Support Library 已被废弃,Google 推荐使用 AndroidX 来替代。

解决方法

  • 在项目的 gradle.properties 文件中增加自动迁移配置,此时重新编译,即可编译通过
	android.useAndroidX=trueandroid.enableJetifier=true
  • android.useAndroidX=true:启用 AndroidX。
  • android.enableJetifier=true:启用 Jetifier 工具,自动迁移项目中的第三方库依赖。

四、配置文件权限

1、确保应用有权限读取 PDF 文件,在 AndroidManifest.xml 中添加以下权限:

	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

2、如果文件存储在外部存储中,还需要动态申请权限:

	// 运行时权限private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->if (isGranted) {// 权限被授予Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show()} else {// 权限被拒绝Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()}}/*** 动态申请权限*/private fun requestPermission1() {requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)}

3、手动申请 “所有文件访问权限”

  • 从 Android 10(API 级别 29)开始,引入了分区存储(Scoped Storage),限制了应用对外部存储的访问权限。只有 READ_EXTERNAL_STORAGE 权限,无法打开 PDF 文件,还需要申请 MANAGE_EXTERNAL_STORAGE 权限,并且手动引导用户手动开启 “所有文件访问权限”。
	/*** 申请 所有文件访问权限*/private fun requestManagerPermission() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {if (!Environment.isExternalStorageManager()) {val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)val applicationId = "com.example.helloworld"//BuildConfig.APPLICATION_IDintent.data = "package:$applicationId".toUri()startActivity(intent)}}}

五、PDFView 常用方法

  • fromFile(File file):从文件路径加载 PDF 文件。
	val filePath = "$filesDir/example.pdf"pdfView.fromFile(new File(filePath )).load();
  • fromAsset(String assetName):从 assets 目录(res/main/assets/example.pdf)加载 PDF 文件。
	pdfView.fromAsset("example.pdf").load();
  • fromUri(Uri uri):从 URI 加载 PDF 文件。

  • defaultPage(int page):设置默认显示的页面。

  • enableSwipe(boolean swipe):启用或禁用滑动翻页。

  • enableDoubletap(boolean doubletap):启用或禁用双击放大。

  • zoom(float scale):设置 PDFView 的初始缩放比例。

  • rotate(int degrees):设置 PDFView 的初始旋转角度。

  • onPageChange(OnPageChangeListener listener):设置页面改变时的监听器。

  • onError(OnErrorListener listener):设置错误监听器。

  • enableAnnotationRendering(boolean enabled):启用注释渲染。

  • setOffscreenPageLimit(int limit):设置预加载页数,避免 OOM 问题。

6、代码示例

相关文章:

  • 7. 进程控制-进程替换
  • 科技项目验收测试对软件产品和企业分别有哪些好处?
  • 填孔即可靠:猎板PCB如何用树脂塞孔重构高速电路设计规则
  • 安装Minikube
  • 芍药BAHD酰基转移酶-文献精读128
  • Vue2项目created不执行
  • Java视频流RTMP/RTSP协议解析与实战代码
  • JavaSE基础语法之方法
  • Hue面试内容整理-Hue 架构与前后端通信
  • LLaMA-Factory微调大模型Qwen2.5
  • 电动汽车直流快充充电桩AEV200-DC240M4的详细介绍
  • mysql-单一的事务--single-transaction选项
  • MyBatis进阶干货知识
  • 海康相机连接测试-极简版
  • K8s CoreDNS 核心知识点总结
  • AI 来袭:颠覆传统的数字营销指南
  • 我用 CodeBuddy 开发了一个颜色命名搜索器 —— ColorNameHub 的诞生记
  • Verilog HDL 语言整理
  • [特殊字符] 如何优雅地避免 SQL 多表 LEFT JOIN 造成的笛卡尔积放大问题?
  • 青少年编程与数学 02-019 Rust 编程基础 16课题、包、单元包及模块
  • 俄乌代表团抵达谈判会场
  • 韶关一企业将消防安装工程肢解发包,广东住建厅:罚款逾五万
  • 外企聊营商|武田制药:知识产权保护助创新药研发
  • 第一集|好饭不怕晚,折腰若如初见
  • 新闻1+1丨城市,如何对青年更友好?
  • 泽连斯基启程前往土耳其