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

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、代码示例

http://www.xdnf.cn/news/6515.html

相关文章:

  • 计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 10.增强表面细节(二)法线贴图
  • SpringCloud微服务开发与实战
  • 官方 Elasticsearch SQL NLPChina Elasticsearch SQL
  • [特殊字符][特殊字符]知识库PHP版 | ChatMoneyAI宝塔面板Docker多部署
  • Java EE初阶——wait 和 notify
  • CentOS高手之路:从进阶实战到企业级优化
  • 维智定位 Android 定位 SDK
  • 网站运维基础 | 2. cms介绍及wordpress的搭建
  • 物联网中的WiFi模式解析:AP、STA与混合模式
  • 【前端优化】vue2 webpack4项目升级webpack5,大大提升运行速度
  • 还没用过智能文档编辑器吗?带有AI插件的ONLYOFFICE介绍
  • 聊聊redisson的RLock的unlock
  • Java微服务架构实战:Spring Boot与Spring Cloud的完美结合
  • Linux 内核中 inet_accept 的实现与自定义传输协议优化
  • 在哪一个终端下运行有影响吗?pip install pillow
  • eVTOL、无人机电机功耗图和电机效率图绘制测试
  • Mendix 中的XPath 令牌(XPath Tokens)详解
  • 低空态势感知:基于AI的DAA技术是低空飞行的重要安全保障-机载端地面端
  • C++ Lambda 表达式介绍
  • 人工智能100问☞第24问:什么是生成对抗网络(GAN)?
  • 互联网应用的安全防线-身份证实名认证api-身份证三要素验证
  • BUUCTF——web刷题第一页题解
  • 【Java实战】IO流(转换流,打印流,数据流,序列化流)
  • Java随机生成邀请码 (包含字母大小写+数字)
  • 2022 Hubei Provincial Collegiate Programming Contest
  • 栈的计算方式和表达方式
  • 【深度剖析】安踏体育的数字化转型(上篇1)
  • 3D曲面上的TSP问题(二):ACO蚁群算法 + 2-opt搜索求解TSP问题
  • 讯联云库项目开发技术栈总结(一)
  • Linux系统发布.net core程序