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

相关推荐
2501_915106324 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息5 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”6 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
局外人LZ1 天前
Uniapp脚手架项目搭建,uniapp+vue3+uView pro+vite+pinia+sass
前端·uni-app·sass
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
前端呆头鹅1 天前
Websocket使用方案详解(uniapp版)
websocket·网络协议·uni-app
浮桥1 天前
uniapp+h5 公众号实现分享海报绘制
uni-app·notepad++
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
wangjun51591 天前
uniapp uni.downloadFile 偶发性下载文件失败 无响应
uni-app