【问题标题】:Cordova: Getting a file Uri from a content UriCordova:从内容 Uri 获取文件 Uri
【发布时间】:2016-09-20 18:42:40
【问题描述】:

我使用 Apache Cordova 实现了一个应用程序,并使用了 $cordovaCamera 插件。 当 $cordovaCamera 的源类型是 Camera.PictureSourceType.PHOTOLIBRARY 时,输出类似于

“内容://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。

【问题讨论】:

    标签: android cordova cordova-plugins ngcordova


    【解决方案1】:

    我发现了问题。 我正在使用 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://

      【讨论】:

        猜你喜欢
        • 2014-06-06
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-01
        • 2014-07-31
        • 2012-08-15
        相关资源
        最近更新 更多