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

相关推荐
RuoyiOffice1 天前
2026 企业定制开发选型:从零开发、低代码、SaaS 与 RuoYi Office 怎么选?
spring boot·uni-app·开源·saas·oa·定制化·ruoyioffice
三天不学习2 天前
【超详细】Vue3+UniApp+.NET8集成腾讯云IM即时通信全攻略
uni-app·.net·腾讯云·im·即时通信
于先生吖2 天前
前后端分离人事招聘项目,校招宣讲预约+社招双向撮合功能架构设计教程
java·开发语言·uni-app
QQ_5110082852 天前
uniapp微信小程序网上饰品商城售卖系统php python物流
微信小程序·uni-app·php
2501_915909062 天前
深入解析Mock.js:功能、应用及实战案例,提升前端开发效率
android·ios·小程序·https·uni-app·iphone·webview
于先生吖2 天前
前后端分离体育服务项目,场馆计费+线下赛事排行小程序部署开发教程
java·小程序·uni-app
蜡台2 天前
Uniapp 实现预览pdf 文件
pdf·uni-app·pdfh5
不想吃饭e2 天前
uniapp-图片,视频上传组件封装
java·uni-app·音视频
2501_916007472 天前
不用 Mac 也可以 Windows下管理iOS描述文件的非Xcode完整指南
android·ios·小程序·https·uni-app·iphone·webview
于先生吖3 天前
UniApp搭配Java后端实现到店预约上门指派,订单状态流转与结算开发教程
java·开发语言·uni-app