gpt4 book ai didi

android - Cordova 插件文件路径 : Unable to resolve filesystem path

转载 作者:行者123 更新时间:2023-12-05 07:26:55 26 4
gpt4 key购买 nike

尝试通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com