uniapp手机根目录
uniapp访问手机根目录的方法
在uniapp中访问手机根目录需要区分不同平台的处理方式,并遵循安全限制。以下是具体实现方案:
安卓平台实现
安卓11及以上版本对文件访问权限有严格限制,需使用uni.saveFile或uni.downloadFile将文件保存到应用私有目录后访问:

uni.downloadFile({
url: 'https://example.com/file.pdf',
success: (res) => {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: (savedRes) => {
console.log('文件保存路径:', savedRes.savedFilePath)
}
})
}
})
iOS平台实现
iOS只能访问应用沙盒内的文件,可通过以下方式获取沙盒路径:

plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
console.log('应用私有目录:', fs.root.fullPath)
})
跨平台通用方案
使用uni-app的扩展API访问特定系统目录(需真机调试):
const path = plus.io.convertLocalFileSystemURL('/')
console.log('转换后的路径:', path)
注意事项
- 访问外部存储需要动态申请权限
- 安卓Manifest需配置
android:requestLegacyExternalStorage="true" - 部分目录需要用户手动授权才能访问
替代方案
对于需要共享的文件,建议使用以下方式:
uni.chooseMessageFile({
count: 1,
type: 'all',
success: (res) => {
console.log('选择的文件:', res.tempFiles[0])
}
})
实际开发中应优先考虑将文件存储在应用私有目录,再通过文件分享功能实现跨应用访问。






