Cordova:从内容Uri获取文件Uri [英] Cordova: Getting a file Uri from a content Uri

查看:369
本文介绍了Cordova:从内容Uri获取文件Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I implemented an app with Apache Cordova and I used $cordovaCamera plugin. When the source type of $cordovaCamera is Camera.PictureSourceType.PHOTOLIBRARY the output is something like

"content://com.android.providers.media.documents/document/image"

"content://com.android.providers.media.documents/document/image"

,当我使用Camera.PictureSourceType.CAMERA时,我得到

and, when I use Camera.PictureSourceType.CAMERA I get

"file:///storage/emulated/0/Android/data/com.ionic.viewapp/cache/image.jpg".

"file:///storage/emulated/0/Android/data/com.ionic.viewapp/cache/image.jpg".

在我的情况下,格式"file://.."更有用.

In my case the format "file://.." is more useful.

是否可以从内容URI获取文件URI?

It is possible get the file URI from the content URI?

我找到了这个问题的许多答案,但是所有解决方案都是针对Java而不是针对Javascript.

I found many answers to this questions but all solutions are for JAVA, not for Javascript.

推荐答案

我发现了问题. 我正在使用Ionic和Ionic View在设备中进行测试.

I found the problem. I'm using Ionic and Ionic View for test in device.

当我尝试 cordova-plugin-filepath 时,它不起作用.但是我发现问题是在Ionic View中此插件失败.

When I tried the cordova-plugin-filepath it didn't work. But I found that the problem was that in Ionic View this plugin fails.

因此,我在服务中编写了此功能:

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

返回我需要的文件URI. 然后,我将apk安装在手机中,并且可以正常工作. 现在,我可以存储从相机或画廊获得的图像,然后上传到服务器.

to return the file URI that I needed it. Then, I installed the apk in the phone, and it works. Now, I can store an image obtained from the camera or gallery and upload to the server.

这篇关于Cordova:从内容Uri获取文件Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆