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

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

一、 添加依赖

  • 模块的 build.gradle 文件中添加以下依赖:
	// pdf
    implementation("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.pom
     Required 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=true
	android.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_ID
	           intent.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、代码示例

Android Studio中集成AndroidPdfViewer以便在新闻APP中下载和显示PDF文件时,可能会遇到多种错误。为了帮助开发者有效解决这些问题,推荐查看资源《Android Studio集成PDF查看器:AndroidPdfViewer详解与问题解决》。这份资料详细介绍了AndroidPdfViewer的使用方法和错误解决方案,对解决当前问题非常有帮助。 参考资源链接:[Android Studio集成PDF查看器:AndroidPdfViewer详解与问题解决](https://wenku.csdn.net/doc/42vpj8txbm?spm=1055.2569.3001.10343) 首先,确保在项目的`build.gradle`文件中正确引入了AndroidPdfViewer及其依赖项。如果遇到找不到插件的错误,如“Plugin with id 'com.github.dcendents.android-maven' not found”,需要在`buildscript`块中添加相应的依赖。此外,还需要确保Gradle版本与AndroidPdfViewer兼容。 在下载PDF文件的过程中,可能会遇到网络权限问题或文件存储权限问题。在Android 6.0及以上版本,需要在运行时请求用户授权网络和存储权限。同时,为了优化用户体验,可以使用DownloadManager来异步下载文件,并使用AndroidPdfViewer提供的API来渲染和显示PDF。在这个过程中,可以通过监听下载进度和显示进度条来反馈给用户。 在显示PDF文件时,可能会遇到文件未下载完成或路径错误等问题。确保文件下载完成后才能初始化PDF viewer,并检查文件路径是否正确。如果用户尝试打开的PDF文件不存在或损坏,应捕获相应的异常,并向用户显示错误信息。 通过上述步骤,可以解决大部分在集成和使用AndroidPdfViewer时遇到的常见错误。如果需要进一步学习关于AndroidPdfViewer的高级功能和深入问题解决方法,建议详细阅读提供的资源《Android Studio集成PDF查看器:AndroidPdfViewer详解与问题解决》,它将为你提供更全面的知识和技巧,帮助你更有效地在Android应用中实现PDF查看功能。 参考资源链接:[Android Studio集成PDF查看器:AndroidPdfViewer详解与问题解决](https://wenku.csdn.net/doc/42vpj8txbm?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值