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);
				}
			}
相关推荐
deli00740 分钟前
Redis 缓存穿透、击穿、雪崩:9 组流量实测数据库怎么被打穿
前端
江华森1 小时前
5Mbps 小水管也能扛:K3s 上 k6 压测 + HPA 弹性伸缩实测
前端
shuangyueliao1 小时前
103 个小游戏的商店素材,我一张图都没手动做:截图 / 封面 / 打包 / 提审全自动
前端
用户2763965765101 小时前
放弃 JWT 后,我给 FastAPI 游戏服务端手撸了一套 token session
javascript
ZhengEnCi1 小时前
前端项目为什么需要一份统一的 theme.css:用 CSS 变量管住全局样式与主题切换
前端·css·前端工程化
wordbaby1 小时前
一次微信小程序 `web-view` 跳转原生页回跳后 H5 白屏的排查与修复(Hash 路由污染)
前端
用户721880438351 小时前
密码骰子:结合AIUI在眼镜上生成一串16位密码
前端
瑞雪兆丰年兮1 小时前
JavaWeb-day14-SpringBootWeb原理篇
java·开发语言·spring boot·spring·bean·mybatis·javaweb
江华森1 小时前
全链路可观测落地记:kube-prometheus-stack 在 K3s 上的 8 个坑与一整套修复脚本
前端
用户84298142418101 小时前
从 JavaScript 到可运行的字节码 - ByteJS 使用指南
javascript