安卓开发,打开PDF文件

devtools/2025/2/7 18:23:31/

1、把PDF文件复制到raw目录下

(1)新建一个Android Resource Directory

(2)Resource type 改成 raw

(3) 把PDF文件复制到raw目录下

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="打开PDF"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="parent" /></LinearLayout>

3、MainActivity.java

java">package com.example.openpdf;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class MainActivity extends AppCompatActivity {String fileName;InputStream inputStream;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button buttonOpenPdf = findViewById(R.id.button1);buttonOpenPdf.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {fileName = "one_one.pdf";inputStream = getResources().openRawResource(R.raw.one_one);request();}});}//检查请求private void request(){//权限检查if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {//如果返回值为true,意味着应用尚未获得该权限ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);}else {checkAndSavePdf();}if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 2);}else {openPdfFile();}}//权限请求结果处理public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == 1) {//如果用户同意授权访问if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {checkAndSavePdf();}else {Toast.makeText(this, "存储权限被拒绝", Toast.LENGTH_SHORT).show();}}if (requestCode == 2) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {openPdfFile();} else {Toast.makeText(this, "读取外部存储权限被拒绝", Toast.LENGTH_SHORT).show();}}}//检查文件是否已经保存private void checkAndSavePdf() {//check检查File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/math"),fileName);if (!pdfFile.exists()) {savePdf();}}//保存PDFprivate void savePdf() {ContentResolver contentResolver = getContentResolver();ContentValues contentValues = new ContentValues();// 设置文件名contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileName);// 设置文件 MIME 类型contentValues.put(MediaStore.Downloads.MIME_TYPE, "application/pdf");if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {// 设置文件相对路径为 Downloads 目录contentValues.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS+"/math");}// 插入文件信息到 MediaStore 并获取 UriUri uri = contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);if (uri != null) {try {// 打开输出流OutputStream outputStream = contentResolver.openOutputStream(uri);if (outputStream != null) {byte[] buffer = new byte[1024];int length;// 从输入流读取数据并写入输出流while ((length = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, length);}// 关闭输出流outputStream.close();// 关闭输入流inputStream.close();}} catch (IOException e) {e.printStackTrace();}}}//打开PDFprivate void openPdfFile() {// 假设 PDF 文件在外部存储的 Downloads 目录下File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS+"/math"),fileName);if (pdfFile.exists()) {Uri uri;// 获取文件的 Uriif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {// 使用 FileProvider 生成 content:// 类型的 Uriuri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", pdfFile);}else {uri = Uri.fromFile(pdfFile);}// 创建隐式意图Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(uri, "application/pdf");intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);}// 检查是否有应用可以处理该意图if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {startActivity(intent);}else {Toast.makeText(this, "没有可用的 PDF 阅读器", Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, "PDF 文件不存在", Toast.LENGTH_SHORT).show();}}}

4、AndroidManifest.xml

在 AndroidManifest.xml 中添加以下代码:

<!-- 声明写入外部存储的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 声明读取外部存储的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><providerandroid:name="androidx.core.content.FileProvider"android:authorities="${applicationId}.fileprovider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" />
</provider>

AndroidManifest.xml 完整代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" ><!-- 声明写入外部存储的权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 声明读取外部存储的权限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.OpenPDF"tools:targetApi="31" ><activityandroid:name=".MainActivity"android:exported="true" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><providerandroid:name="androidx.core.content.FileProvider"android:authorities="${applicationId}.fileprovider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" /></provider></application></manifest>

5、创建 file_paths.xml 文件 

在 res/xml 目录下创建 file_paths.xml 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android"><external-path name="external_files" path="." />
</paths>

3、


http://www.ppmy.cn/devtools/156898.html

相关文章

DeepSeek 遭 DDoS 攻击背后:DDoS 攻击的 “千层套路” 与安全防御 “金钟罩”

当算力博弈升级为网络战争&#xff1a;拆解DDoS攻击背后的技术攻防战——从DeepSeek遇袭看全球网络安全新趋势 在数字化浪潮席卷全球的当下&#xff0c;网络已然成为人类社会运转的关键基础设施&#xff0c;深刻融入经济、生活、政务等各个领域。从金融交易的实时清算&#xf…

如何使用Webpack构建前端应用?

在现代前端开发中&#xff0c;Webpack已成为构建工具的主流选择&#xff0c;它能够帮助开发者打包和优化应用程序的资源。通过Webpack&#xff0c;前端应用的构建流程变得更加高效和灵活&#xff0c;无论是模块化开发&#xff0c;还是性能优化&#xff0c;都能够轻松实现。本文…

【C语言篇】“三子棋”

一、游戏介绍 三子棋&#xff0c;英文名为 Tic - Tac - Toe&#xff0c;是一款简单而经典的棋类游戏。游戏在一个 33 的棋盘上进行&#xff0c;两名玩家轮流在棋盘的空位上放置自己的棋子&#xff08;通常用 * 和 # 表示&#xff09;&#xff0c;率先在横、竖或斜方向上连成三个…

Android学习19 -- 手搓App

1 前言 之前工作中&#xff0c;很多时候要搞一个简单的app去验证底层功能&#xff0c;Android studio又过于重型&#xff0c;之前用gradle&#xff0c;被版本匹配和下载外网包折腾的堪称噩梦。所以搞app都只有找应用的同事帮忙。一直想知道一些简单的app怎么能手搓一下&#x…

Kali Linux 渗透测试环境配置(Metasploit + Burp Suite)

一、Kali Linux 系统准备 首先&#xff0c;确保你已经成功安装了 Kali Linux 系统。可以从官方网站下载镜像文件&#xff0c;并通过 U 盘引导安装等常规方式完成系统部署。建议使用最新稳定版本&#xff0c;以获取最新的软件包支持和安全更新。 安装完成后&#xff0c;登录系…

自指学习:AGI的元认知突破

文章目录 引言:从模式识别到认知革命一、自指学习的理论框架1.1 自指系统的数学定义1.2 认知架构的三重反射1.3 与传统元学习的本质区别二、元认知突破的技术路径2.1 自指神经网络架构2.2 认知效能评价体系2.3 知识表示的革命三、实现突破的关键挑战3.1 认知闭环的稳定性3.2 计…

大数据sql查询速度慢有哪些原因

1.索引问题 可能缺少索引&#xff0c;也有可能是索引不生效 2.连接数配置&#xff1a;连接数过少/连接池比较小 连接数过 3.sql本身有问题&#xff0c;响应比较慢&#xff0c;比如多表 4.数据量比较大 -这种最好采用分表设计 或分批查询 5.缓存池大小 可能是缓存问题&#xff…

【Elasticsearch】 Composite Aggregation 详解

1.什么是 Composite Aggregation&#xff1f; Composite Aggregation 是 Elasticsearch 中的一种特殊聚合方式&#xff0c;适用于需要分页展示的聚合结果。它与传统的聚合方式不同&#xff0c;采用了基于游标的分页模型。这种聚合方式可以高效地处理多级聚合中的所有桶&#x…