uniapp app端获取指定路径下的所有图片

以大疆图片存储路径为例,获取该路径下的所有图片:

javascript 复制代码
let that = this
// Android微博图片路径
// const targetDirPath = '/storage/emulated/0/Pictures/Weibo/';
// Android大疆图片存储路径
const targetDirPath = '/storage/emulated/0/DCIM/DJI Album/'; 
// 步骤1:解析文件夹路径,获取DirectoryEntry对象
plus.io.resolveLocalFileSystemURL(targetDirPath, function(dirEntry) {
	// 步骤2:获取文件夹读取器DirectoryReader
	const dirReader = dirEntry.createReader();
	// 步骤3:读取文件夹中的所有文件/子文件夹
	dirReader.readEntries(function(entries) {
	    // entries是数组,包含当前文件夹下的FileEntry(文件)和DirectoryEntry(子文件夹)
	    const imageFiles = []; // 存储筛选后的图片文件
	    // 步骤4:遍历并筛选图片文件
	    entries.forEach(function(entry) { //所有的图片
		    // 只处理文件(排除子文件夹)
		    if (entry.isFile) {
			    const fileName = entry.name.toLowerCase(); // 转为小写,兼容后缀大小写
			    // 筛选图片格式(png/jpg/jpeg)
			    if (fileName.endsWith('.png') || fileName.endsWith('.jpg') ||
			    	fileName.endsWith('.jpeg')) {
			    	let imgPath = entry.fullPath
				    imageFiles.push({
					    name: entry.name,
					    path: entry.fullPath, // 文件绝对路径
					    url: entry.toURL(), // 文件本地URL(可用于预览)
				    })						
			    }
		    }
	    });
	    // 最终得到的图片文件列表
	    console.log('文件夹下的图片列表:', imageFiles);
    }, function(err) {
	    console.error('读取文件夹内容失败:', err);
    });
}, function(err) {
	console.error('解析文件夹路径失败:', err);
	// 失败原因:路径不存在、无权限、iOS沙盒限制等
});​​

打印出来的imageFiles如下:

相关推荐
xiaopang8 小时前
一次小程序 Canvas 海报真机调试复盘:从空白导出到 iOS 保存失败
uni-app
MrFlySand_飞沙12 小时前
uniapp开发微信小程序实现接入企业微信客服
微信小程序·小程序·uni-app·企业微信
PedroQue991 天前
Vue Router 4.x风格导航守卫全面升级
前端·uni-app
Haibakeji1 天前
APP小程序定制开发项目频繁延期?前期规避方法与落地避坑指南
小程序·uni-app·软件需求
anyup2 天前
迁移uni-app x,我是如何让 AI 把我一步步搞崩溃的...
前端·uni-app·trae
2501_916007472 天前
申请 iOS 推送证书并配置 APNs 群发推送教程
android·ios·小程序·https·uni-app·iphone·webview
博客zhu虎康2 天前
uni-app云打包显示编译成功,但是一直没有进入打包队列,后面也一直没反应
uni-app
JavaDog程序狗2 天前
【指南】uni-app微信小程序多环境CI-CD完全指南
ci/cd·uni-app·jenkins
PedroQue993 天前
uni-router v2.1.0 升级:导航守卫全面支持返回值模式
前端·uni-app
2501_915909063 天前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview