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如下:

相关推荐
优雅的大白鹅19 小时前
创建uniapp小程序
小程序·uni-app
笨笨狗吞噬者2 天前
uni-app 运行时揭秘:styleIsolation 的转化
前端·微信小程序·uni-app
神の愛2 天前
JeecgBoot-Uniapp
uni-app
怀君2 天前
Uniapp——微信小程序Canvas层级过高问题解决
微信小程序·小程序·uni-app
阿凤213 天前
uniapp运行到app端怎么打开文件
android·前端·javascript·uni-app
00后程序员张3 天前
完整教程:如何将iOS应用程序提交到App Store审核和上架
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张3 天前
iOS应用性能优化全解析:卡顿、耗电、启动与瘦身
android·ios·性能优化·小程序·uni-app·iphone·webview
Front思3 天前
解决 uniapp Dart Sass 2.0.0 弃用警告
前端·uni-app·sass
星空下的曙光3 天前
uniapp编译到微信小程序接口获取不到数据uni.request
微信小程序·小程序·uni-app
2501_916007474 天前
iOS逆向工程:详细解析ptrace反调试机制的破解方法与实战步骤
android·macos·ios·小程序·uni-app·cocoa·iphone