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.mzph.cn/pingmian/81348.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序学习之搜索框

1、第一步&#xff0c;我们在index.json中引入vant中的搜索框控件&#xff1a; {"usingComponents": {"van-search": "vant/weapp/search/index"} } 2、第二步&#xff0c;直接在index.wxml中添加布局&#xff1a; <view class"index…

OpenCL C++ 常见属性与函数

核心对象与属性 对象/属性描述示例cl::Platform表示OpenCL平台cl::Platform::get(&platforms)cl::Device表示计算设备cl::Device::getDefault()cl::Context管理设备、内存和命令队列的上下文cl::Context(contextDevices)cl::CommandQueue命令队列,用于提交命令cl::Command…

Milvus 视角看重排序模型(Rerankers)

在信息检索和生成式人工智能领域&#xff0c;重排序器是优化初始搜索结果顺序的重要工具。重排序器与传统的嵌入模型不同&#xff0c;它将查询和文档作为输入&#xff0c;并直接返回相似度得分&#xff0c;而不是嵌入。该得分表示输入查询和文档之间的相关性。 重排序器通常在…

C语言:gcc 如何调用 Win32 打开文件对话框 ?

在 Windows 平台上使用 gcc 调用原生 Win32 API 实现文件打开对话框是可行的&#xff0c;但需要直接使用 Win32 的 GetOpenFileName 函数&#xff08;位于 commdlg.h 头文件&#xff0c;依赖 comdlg32.lib 库&#xff09;。以下是完整实现步骤和代码示例&#xff1a; 编写 file…

计算机视觉与深度学习 | Python实现EMD-SSA-VMD-LSTM时间序列预测(完整源码和数据)

EMD-SSA-VMD-LSTM混合模型 一、环境配置与依赖二、数据生成&#xff08;示例数据&#xff09;三、多级信号分解1. 经验模态分解&#xff08;EMD&#xff09;2. 奇异谱分析&#xff08;SSA&#xff09;3. 变分模态分解&#xff08;VMD&#xff09; 四、数据预处理1. 归一化处理2…

vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容

文章目录 一、路由链路二、实现步骤准备二级路由下的.vue文件配置子路由声明router-view标签为菜单项 el-menu-item 设置index属性&#xff0c;设置点击后的路由路径 三、参考资料 一、路由链路 二、实现步骤 准备二级路由下的.vue文件 配置子路由 router/index.js import {…

ModuleNotFoundError: No module named ‘SDToolbox‘

(py311) C:>python Python 3.11.11 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:34:19) [MSC v.1929 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. from SDToolbox import PostShock_eq Tracebac…

Hi3516DV500刷写固件

hi3516DV500刷固件 1、硬件连接 2、软件准备 3、刷固件步骤 一、硬件连接 特别注意的是&#xff0c;串口的接线顺序 通过网线连接好笔记本和开发板后&#xff0c;需要确认一下网口水晶头是否闪烁&#xff0c;以确认网络物理是否连通 二、软件资源准备 固件包准备 打开工具…

正则表达式r前缀使用指南

正则表达式中的 r&#xff1a;解锁字符串转义的魔法 正则表达式是处理字符串的强大工具&#xff0c;但它常常伴随着转义字符的复杂性。如果你曾因 \n、\t 或 \\ 的使用而困惑&#xff0c;那么这篇文章将为你揭开谜底&#xff0c;解释为什么 r 是正则表达式中的「神奇武器」。本…

网络攻防模拟:城市安全 “数字预演”

在当今数字化快速发展的时代&#xff0c;网络安全和城市安全面临着前所未有的挑战。为有效应对这些挑战&#xff0c;利用先进的技术搭建模拟演练平台至关重要。图扑软件的 HT for Web 技术&#xff0c;为网络攻防模拟与城市安全演练提供了全面且高效的解决方案。 三维场景搭建&…

AI模型开发全流程笔记

一、训练数据准备阶段 数据采集标准 格式要求&#xff1a;严格QA对形式&#xff08;1问1答&#xff09; 数量基准&#xff1a; 基础量&#xff1a;500组QA对 优化量&#xff1a;800-1000组QA对 内容规范&#xff1a; 聚焦单一业务节点&#xff08;如售后场景&#xff09; …

1688 数据接口调用秘籍:高效获取商品实时信息的开发指南

在电商行业竞争白热化的当下&#xff0c;企业想要抢占市场先机&#xff0c;实时掌握商品信息至关重要。作为国内 B2B 电商巨头&#xff0c;1688 平台汇聚海量商品资源&#xff0c;通过高效调用其数据接口获取商品实时信息&#xff0c;能为企业价格策略制定、库存管理、竞品分析…

milvus学习笔记

本文主要由AI生成&#xff0c;请注意自己查看源代码校验。 Milvus v2.4 系统架构概览 Milvus 采用分布式微服务架构&#xff0c;将计算层&#xff08;Proxy、QueryCoord、QueryNode、IndexCoord、DataCoord、DataNode 等&#xff09;与存储层&#xff08;Pulsar、MinIO/S3、e…

使用教程:8x16模拟开关阵列可级联XY脚双向导通自动化接线

以下通过点亮LED进行基本使用流程演示&#xff0c;实际可以连接复杂外设&#xff08;SPI、CAN、ADC等&#xff09; 单模块使用 RX、TX、5V和GND接到串口模块&#xff1b;X5接5V&#xff1b;Y2接LED;LED-接GND 串口模块插上电脑后&#xff0c;LED没有亮&#xff1b;因为此时模…

HarmonyOS NEXT~鸿蒙应用上架指南:HarmonyOS应用发布全流程解析

HarmonyOS NEXT&#xff5e;鸿蒙应用上架指南&#xff1a;HarmonyOS应用发布全流程解析 引言 随着华为鸿蒙操作系统(HarmonyOS)生态的快速发展&#xff0c;越来越多的开发者希望将自己的应用上架到鸿蒙应用市场。本文将详细介绍鸿蒙应用上架的全流程&#xff0c;帮助开发者顺…

20250517 我设想一个空间,无限大,空间不与其中物质进行任何作用,甚至这个空间能容纳可以伸缩的空间

1.我设想一个空间&#xff0c;无限大&#xff0c;空间不与其中物质进行任何作用&#xff0c;甚至这个空间能容纳可以伸缩的空间 您设想的这个空间具有一些有趣的特点&#xff1a; 无限大&#xff1a;空间本身没有边界或限制&#xff0c;理论上可以容纳无限多的物质或结构。非…

使用 Kaniko来构建镜像

使用 Kaniko来构建镜像 Kaniko 是一种专注于容器镜像构建的开源工具&#xff0c;其核心设计理念与 Docker 存在显著差异。以下从功能定位、技术实现和适用场景三方面进行对比分析&#xff1a; 一、Kaniko 的核心特性 无需 Docker 守护进程 Kaniko 直接在容器或 Kubernetes 集…

webman用nginx代理静态json文件的异步跨域

场景 有.json文件置于webman的public目录下&#xff0c;使用了nginx做代理&#xff0c;直接访问文件是可以正常加载的&#xff0c;但跨域浏览器就无法加载文件。 nginx配置 文件是否存在于跟目录&#xff0c;存在则设置请求头&#xff0c;不存在则将请求交给webman处理即可。…

JDK 21新特性全面解析

Java Development Kit (JDK) 21作为Oracle长期支持(LTS)版本&#xff0c;于2023年9月正式发布&#xff0c;带来了多项令人振奋的新特性和改进。本文将全面介绍JDK 21中的主要更新&#xff0c;帮助开发者了解如何利用这些新功能提升开发效率和代码质量。 一、虚拟线程(Virtual …

如何选择高性价比的 1T 服务器租用服务​

选择高性价比的 1T 服务器租用服务​&#xff0c;可参考以下内容&#xff1a; 1、根据需求选配置​ 明确自身业务需求是关键。若为小型网站或轻量级应用&#xff0c;数据存储与处理需求不高&#xff0c;选择基础配置服务器即可。如个人博客网站&#xff0c;普通的 Intel Xeon …