简体   繁体   English

cordova-plugin-filepath:无法解析文件系统路径

[英]cordova-plugin-filepath: Unable to resolve filesystem path

Trying to upload image from ionic application using cordova-plugin-camera via android/ios gallery.尝试通过 android/ios 库使用cordova-plugin-camera从 ionic 应用程序上传图像。 It's working perfectly fine on ios but throw error while resolving path, I'm using cordova-plugin-filepath for resolving file path.它在 ios 上工作得非常好,但是在解析路径时抛出错误,我正在使用cordova-plugin-filepath来解析文件路径。

But it always throw following error while resolving native path in this.filePath.resolveNativePath(imagePath) method:但是在this.filePath.resolveNativePath(imagePath)方法中解析本机路径时总是会抛出以下错误:

{code: 0 ; message: "Unable to resolve filesystem path."}

Here is my code for uploading image:这是我上传图片的代码:

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);
});

I have even tried using following code but no success:我什至尝试使用以下代码但没有成功:

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)
      });

Here are my plugin details:这是我的插件详细信息:

<plugin name="cordova-plugin-camera" spec="^4.0.3" />
<plugin name="cordova-plugin-filepath" spec="^1.4.2" />

Ionic info:离子信息:

ionic (Ionic CLI): 4.6.0 Ionic Framework: ionic-angular 3.9.2 @ionic/app-scripts: 3.2.1 Cordova: cordova (Cordova CLI): 8.1.2 (cordova-lib@8.1.1) Cordova Platforms: android 7.1.4 Cordova Plugins: cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.3.1, (and 15 other plugins) System: Android SDK Tools: 25.2.4 NodeJS: v9.11.1 npm: 6.0.1 OS: Windows 10"

Just added the file:// to the imagePath will do.只需将file://添加到 imagePath 即可。

Code Snippet代码片段

 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); });

Explanation解释

The previous code u write only available for the android version below android version 10.For the android version 10 and above couldn't recognized the file-path.你之前写的代码只适用于 android 版本 10 以下的 android 版本。对于 android 版本 10 及以上版本无法识别文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM