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.pswp.cn/pingmian/81348.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/81348.shtml
英文地址,請注明出處:http://en.pswp.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 …