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

相关推荐
宸翰20 小时前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
时光足迹2 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
时光足迹2 天前
腾讯云 TRTC UniApp SDK 从入门到上线
前端·vue.js·uni-app
时光足迹2 天前
uni-app 里把加密视频嵌入页面播放?我分析了 4 种方案,只有 1 种接近完美
前端·vue.js·uni-app
时光足迹2 天前
JPush UniApp UTS 插件完全参考手册:API、事件与厂商通道一网打尽
vue.js·ios·uni-app
时光足迹2 天前
极光推送全攻略(下):uni-app 代码实现与 iOS 排查实战
vue.js·ios·uni-app
时光足迹2 天前
极光推送全攻略(上):被iOS证书折磨了三天,我写了一份前端也能看懂的避坑指南
前端·ios·uni-app
spmcor4 天前
身份证读卡“无感登录”方案实践:从手动点击到自动检测
uni-app
PedroQue994 天前
uni-router v1.8.0新增冷启动守卫补执行
前端·uni-app
PedroQue995 天前
uni-router v1.7.0重磅更新:守卫重定向自由掌控
前端·uni-app