简体   繁体   English

Cordova:从内容Uri获取文件Uri

[英]Cordova: Getting a file Uri from a content Uri

I implemented an app with Apache Cordova and I used $cordovaCamera plugin. 我使用Apache Cordova实现了一个应用程序,并使用了$ cordovaCamera插件。 When the source type of $cordovaCamera is Camera.PictureSourceType.PHOTOLIBRARY the output is something like 当$ cordovaCamera的源类型为Camera.PictureSourceType.PHOTOLIBRARY时,输出类似于

"content://com.android.providers.media.documents/document/image" “内容://com.android.providers.media.documents/document/image”

and, when I use Camera.PictureSourceType.CAMERA I get 当我使用Camera.PictureSourceType.CAMERA时

"file:///storage/emulated/0/Android/data/com.ionic.viewapp/cache/image.jpg". “文件:///storage/emulated/0/Android/data/com.ionic.viewapp/cache/image.jpg”。

In my case the format "file://.." is more useful. 在我的情况下,格式“ file:// ..”更有用。

It is possible get the file URI from the content URI? 是否可以从内容URI获取文件URI?

I found many answers to this questions but all solutions are for JAVA, not for Javascript. 我找到了许多关于这个问题的答案,但是所有解决方案都是针对JAVA而不是Javascript。

I found the problem. 我发现了问题。 I'm using Ionic and Ionic View for test in device. 我正在使用Ionic和Ionic View在设备中进行测试。

When I tried the cordova-plugin-filepath it didn't work. 当我尝试cordova-plugin-filepath时,它不起作用。 But I found that the problem was that in Ionic View this plugin fails. 但是我发现问题是在Ionic View中此插件失败。

So, I wrote in a service this function: 因此,我在服务中编写了此功能:

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

to return the file URI that I needed it. 返回我需要的文件URI。 Then, I installed the apk in the phone, and it works. 然后,我将apk安装在手机中,并且可以正常工作。 Now, I can store an image obtained from the camera or gallery and upload to the server. 现在,我可以存储从相机或画廊获得的图像,然后上传到服务器。

There is an error in answer above. 上面的答案有误。

After window.FilePath.resolveNativePath succeed, its result already has format: file:///... , so no need to prepend file:// . window.FilePath.resolveNativePath成功之后,其结果已经具有格式: file:///... ,因此无需在file://

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

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