2017-04-24 26 views
2

我做过this教程。Ionic2 - 无法在给定路径的UI上显示本地图像

一切正常,但从图库中选择图像时,图像uri已通过其属性srcimg[src])分配给img元素。然后图像显示为破损。

我记录的图像URI,得到了以下几点:

content://com.android.providers.media.documents/document/image%3A1792 

然后,只是为了测试我的真实路径overrided的url,也得到了一个破碎的形象。这是真正的路径我想:

/storage/9016-4EF8/DCIM/Camera/20170422_202334.jpg 

然后,我的问题是: 我如何获得的图像显示得到使用其路径ionic2 UI

也许问题是嵌入式webview(如正常浏览器)(Ionic/Cordova)没有权限显示本地媒体的安全原因?在这种情况下,应该是解决这个问题的一种方式或插件?

在相机subexample上面的教程工作确定,因为什么被分配给该图像元素是图像的base64格式的内容,但我想利用一个path的,因为我觉得它更清楚。

我尝试了以下链接的一些建议,但没有成功。也许是一个旧的链接不适合当前的平台(或者建议是好的,我犯了一些错误)。 https://forum.ionicframework.com/t/no-local-image-showing-in-view-on-device/30647

这里有我的系统的一些信息:

$ npm -v 
3.10.10 

$ cordova -v 
6.5.0 

$ ionic info  
Your system information:  
Cordova CLI: 6.5.0 
Ionic CLI Version: 2.2.3 
Ionic App Lib Version: 2.2.1 
ios-deploy version: Not installed 
ios-sim version: Not installed 
OS: Windows 10 
Node Version: v6.10.2 
Xcode version: Not installed 

如何得到这个工作,你知道吗?

回答

0

此问题是由于在android中使用sourceType: CAMERA. PHOTOLIBRARY uri无法在img src属性中显示。要解决此问题,请使用以下代码更正图像路径。

Camera.getPicture(options).then((imagePath) => { 
    // Correcting android photolibrary image page 
    this.nativeFilePath = imagePath; 
    if (this.pl.is('android') && sourceType === Camera.PictureSourceType.PHOTOLIBRARY) { 
     FilePath.resolveNativePath(imagePath) 
     .then(filePath => { 
      this.nativeFilePath = filePath; 
      let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?')); 
     }); 
    } else { 
     var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1); 
    } 
    }, err => { 
    // this.presentToast('upload image fail.'); 
    }); 

希望这有助于!