Cordova:从内容Uri获取文件Uri

4
我使用Apache Cordova实现了一个应用,并使用了$cordovaCamera插件。 当$cordovaCamera的源类型为Camera.PictureSourceType.PHOTOLIBRARY时,输出结果类似于“content://com.android.providers.media.documents/document/image”。 而当我使用Camera.PictureSourceType.CAMERA时,输出结果是“file:///storage/emulated/0/Android/data/com.ionic.viewapp/cache/image.jpg”。 在我的情况下,“file://…”格式更有用。 是否可以从内容URI获取文件URI? 我找到了许多答案,但所有解决方案都是针对JAVA,而不是JavaScript。
2个回答

6
我来翻译一下:

我找到了问题所在。 我正在使用 Ionic 和 Ionic View 在设备上进行测试。

当我尝试使用 cordova-plugin-filepath 时,它没有起作用。但我发现问题出在 Ionic View 上这个插件失败了。

因此,我在一个服务中编写了这个函数:

// data is the return of $cordovaCamera.getPicture.

    getFilePath: function(data){ 
             var deferred = $q.defer();
             window.FilePath.resolveNativePath(data, function(result) {
                    deferred.resolve('file://' + result;);
                  }, function (error) {
                      throw new Error("");
                  });

             return deferred.promise;
         }

返回我所需的文件URI。然后,我在手机上安装了apk,并且它可以正常工作。现在,我可以存储从相机或图库获取的图像并上传到服务器。


2
上面的答案有错误。
在成功执行window.FilePath.resolveNativePath之后,其结果已经具有格式:file:///...,因此不需要再添加file://

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接