尝试通过 android/ios 库使用 cordova-plugin-camera 从 ionic 应用程序上传图像。 它在 ios 上工作得很好,但在解析路径时抛出错误,我正在使用 cordova-plugin-filepath 来解析文件路径。
但在 this.filePath.resolveNativePath(imagePath) 方法中解析 native 路径时,它总是抛出以下错误:
{code: 0 ; message: "Unable to resolve filesystem path."}
这是我上传图片的代码:
var options = {
quality: 60,
targetWidth:900,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
console.log('image path',imagePath)
this.filePath.resolveNativePath(imagePath)
.then(res => {
let correctPath = res.substr(0, res.lastIndexOf('/') + 1).toString();
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}).catch(err=>{
console.log('unable to resolve file path issue', err)
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
console.log(currentName,correctPath)
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
console.log(err);
});
我什至尝试使用以下代码但没有成功:
window.FilePath.resolveNativePath(imagePath)
.then(res => {
let correctPath = res.substr(0, res.lastIndexOf('/') + 1).toString();
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}).catch(err=>{
console.log('unable to resolve file path issue', err)
});
这是我的插件详细信息:
<plugin name="cordova-plugin-camera" spec="^4.0.3" />
<plugin name="cordova-plugin-filepath" spec="^1.4.2" />
ionic 信息:
ionic ( ionic CLI):4.6.0
ionic 框架: ionic Angular 3.9.2
@ionic/应用程序脚本:3.2.1
Cordova :
Cordova ( Cordova CLI):8.1.2( Cordova -lib@8.1.1)
Cordova 平台:android 7.1.4
Cordova 插件:cordova-plugin-ionic-keyboard 2.1.3、cordova-plugin-ionic-webview 2.3.1(和其他 15 个插件)
系统:
安卓 SDK 工具:25.2.4
节点JS:v9.11.1
npm:6.0.1
操作系统:Windows 10"
最佳答案
只需将 file://
添加到 imagePath 即可。
代码 fragment
this.camera.getPicture(options).then((imagePath) => {
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
//insert this line will solve the error
imagePath = 'file://' + imagePath;
console.log('image path',imagePath)
this.filePath.resolveNativePath(imagePath)
.then(res => {
let correctPath = res.substr(0, res.lastIndexOf('/') + 1).toString();
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}).catch(err=>{
console.log('unable to resolve file path issue', err)
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
console.log(currentName,correctPath)
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
console.log(err);
});
解释
你之前写的代码只适用于android版本9以下的android版本。对于android版本9及以上无法识别文件路径。
关于android - Cordova 插件文件路径 : Unable to resolve filesystem path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54182550/
相关文章:
flutter - 为什么 build() 在后台的屏幕上被多次调用?
apache-spark - 我的 SparkSession 初始化需要很长时间才能在我的笔记本电脑
r - 使用 R 中的 DBI 和 ActiveDirectoryPassword 身份验证连接到
reactjs - 用于 Web 的 React 向上滑动面板
reactjs - 从 DateInput BlueprintJS 更改月份名称
python - gremlin-python : what is a valid vertex c
sql-server - 来自 Azure 网站的 Azure VM SQL Server Inte