uniapp+vue开发app,蓝牙连接,蓝牙接收文件保存到手机特定文件夹,从手机特定目录(可自定义),读取文件内容,这篇首先说如何读取,手机目录如何寻找

首先说一下研究思路,时间紧急不看的直接拿取粉色代码部分即可

1.首先要解决第一个问题就是手机目录是什么,文件路径如何写,参考了一篇文章中的这一句话

标蓝


//Android平台本地绝对路径为"storage/emulated/0",就是用户文件管理器能看到的了;

filename: 'file://storage/emulated/0/xxx/'


所以尝试了以下方法

// plus.io.resolveLocalFileSystemURL(url,function(entry){

// console.log("entry",entry)

// entry.file(function(file){

// var reader=new plus.io.FileReader()

// reader.onloadend=function(e){

// console.log('filecontent',e.target.result)

// }

// reader.readAsText(file,'utf-8')

// })

// })

发现,不行,后得知, 这种方式手机版本10+就不行了

现在是需要将路径的file://去掉,根目录就是/storage/,到0为手机文件管理可以看到的路径了,以下为微信下载的文件举例

const filePath = '/storage/emulated/0/Download/WeiXin/shuju.dat';

// 替换为你的实际文件路径
this.ceshidata= readFileContent(filePath);

安卓10+以下系统获取文件目录

通过5+ 提供的api获取文件目录 plus.io.resolveLocalFileSystemURL

安卓10+以上系统获取文件目录

通过引入安卓原生类 File 获取文件列表及文件

// 导入 java.io.File 类 var File = plus.android.importClass('java.io.File');

// 创建 File 对象,指定目录路径 var dir = new File(dirPath);

// 获取目录下的文件列表 var files = dir.listFiles();

2.完整的读取代码

复制代码
	function readFileContent(filePath) {
				try {
					// 导入必要的类
					var File = plus.android.importClass('java.io.File');
					var FileInputStream = plus.android.importClass('java.io.FileInputStream');
					var InputStreamReader = plus.android.importClass('java.io.InputStreamReader');
					var BufferedReader = plus.android.importClass('java.io.BufferedReader');

					// 创建File对象
					var file = new File(filePath);
					// 输出更多调试信息
					console.log('Attempting to read file at path: ' + filePath);
					console.log('File exists: ' + file.exists());
					console.log('Is file: ' + file.isFile());
					console.log('Absolute path: ' + file.getAbsolutePath());
					if (file.exists() && file.isFile()) {
						// 打开文件输入流
						var fis = new FileInputStream(file);
						var isr = new InputStreamReader(fis, "UTF-8"); // 指定编码格式
						var br = new BufferedReader(isr);

						var content = '';
						var line;
						while ((line = br.readLine()) !== null) {
							content += line + '\n';
						}

						// 关闭资源
						br.close();
						isr.close();
						fis.close();

						// 输出文件内容到控制台或做其他处理
						console.log('File: ' + file.getName() + ', Content: ' + content);
						
						return content; // 或者你可以选择返回文件内容
					} else {
						console.log('The specified file does not exist or is not a regular file.');
					}
				} catch (e) {
					console.error('Error reading file: ' + e.message);
				}
			}
相关推荐
艾莉丝努力练剑27 分钟前
【AI大模型接入SDK】解决Unicode码转化以及界面变形问题
前端·c++·人工智能·学习·安全·架构
Risehuxyc2 小时前
PHP中如何将字符串转成字节数组,再异或成一个字节
开发语言·php
饮水桶_T3 小时前
C++ STL00 | 六大核心组件协作框架 笔记
开发语言·算法·百度·facebook·新浪微博
悠仁さん3 小时前
【C++】unordered_map_set 模拟实现
开发语言·c++
1-1=03 小时前
Vue 全局事件总线
前端·javascript·vue.js
唐老板4 小时前
前端和后端,转 AI 全栈怎么转
前端·后端
AI前端工程狮4 小时前
我给 MCP 工具加了删除确认,用户点了拒绝,数据还是删了
javascript·人工智能·程序员
用户298698530144 小时前
在 React 中实现 Excel 文本与数字互转的完整实践
javascript·react.js·excel
Risehuxyc5 小时前
为什么php结束符?>省略不写
开发语言·php
程序员鱼皮5 小时前
Antigravity 保姆级实战教程,从 0 到 1 万字干货!
前端·后端·ai编程