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

Android加载pdf

依赖

implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

在project.build中添加该源

maven { url "https://repository.liferay.com/nexus/content/repositories/public/" }

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.github.barteksc.pdfviewer.PDFViewandroid:id="@+id/pdfView"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>

Activity

import android.os.Bundle;
import android.os.Environment;
import androidx.appcompat.app.AppCompatActivity;
import com.github.barteksc.pdfviewer.PDFView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;public class MainActivity extends AppCompatActivity {private PDFView pdfView;private static final String PDF_URL = "https://www.example.com/sample.pdf"; // 替换为你的 PDF URL@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pdfView = findViewById(R.id.pdfView);downloadAndDisplayPdf(PDF_URL);}private void downloadAndDisplayPdf(String url) {OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url(url).build();client.newCall(request).enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {e.printStackTrace();}@Overridepublic void onResponse(Call call, Response response) throws IOException {if (response.isSuccessful()) {File pdfFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "downloaded.pdf");try (InputStream inputStream = response.body().byteStream();FileOutputStream outputStream = new FileOutputStream(pdfFile)) {byte[] buffer = new byte[2048];int length;while ((length = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, length);}// 在主线程中加载 PDFrunOnUiThread(() -> pdfView.fromFile(pdfFile).load());}}}});}
}
PDF不支持在线预览,因此要先下载后加载•	下载 PDF 文件:使用 OkHttp 请求 PDF 的在线地址,并将其保存到设备的下载目录中。
•	加载 PDF 文件:下载完成后,通过 pdfView.fromFile(pdfFile).load() 在 PDFView 中显示 PDF 文件。
http://www.lryc.cn/news/484316.html

相关文章:

  • IOT物联网低代码可视化大屏解决方案汇总
  • Python的面向对象day7
  • 计算机网络(11)和流量控制补充
  • Rust 所有权机制
  • Pwn VM writeup
  • LSTM(长短期记忆网络)详解
  • 机器学习 贝叶斯公式
  • Scala-注释、标识符、变量与常量-用法详解
  • 大数据学习14之Scala面向对象--至简原则
  • docker 安装之 windows安装
  • JS 实现游戏流畅移动与按键立即响应
  • LabVIEW大数据处理
  • NVR录像机汇聚管理EasyNVR多品牌NVR管理工具视频汇聚技术在智慧安防监控中的应用与优势
  • 海思3403对RTSP进行目标检测
  • Vue之插槽(slot)
  • 分布式服务高可用实现:复制
  • 基于yolov8、yolov5的车型检测识别系统(含UI界面、训练好的模型、Python代码、数据集)
  • 机器学习—决定下一步做什么
  • Java Optional详解:避免空指针异常的优雅方式
  • SpringBoot开发——整合EasyExcel实现百万级数据导入导出功能
  • AcWing 1097 池塘计数 flood fill bfs搜索
  • R门 - rust第一课陈天 -内存知识学习笔记
  • java itext后端生成pdf导出
  • 信号-3-信号处理
  • 38配置管理工具(如Ansible、Puppet、Chef)
  • 网络技术-定义配置ACL规则的语法和命令
  • 动态规划一>子数组系列
  • 一觉睡醒,全世界计算机水平下降100倍,而我却精通C语言——scanf函数
  • Altium Designer使用技巧(五)
  • Docker 的安装与使用