简体   繁体   English

Cordova 3.4.0:从GALLERY中选择时,Camera.getPicture()返回编码的URI

[英]Cordova 3.4.0: Camera.getPicture() returns encoded URI when selected from GALLERY

I am using Camera.getPicture() API to capture image or select image from GALLERY. 我正在使用Camera.getPicture()API来捕获图像或从GALLERY中选择图像。 When I take the picture using camera, it returns me FileEntry having correct URL with filename and extension. 当我使用相机拍摄照片时,它会返回FileEntry,其中包含带有文件名和扩展名的正确URL。 But when I select a file from Gallery, it returns "FileEntry.fullPath" as /com.android.providers.media.documents/document/image%3A322 and sometimes /media/external/images/media/319 但是当我从Gallery中选择一个文件时,它会将“FileEntry.fullPath”返回为/com.android.providers.media.documents/document/image%3A322,有时还会返回/ media / external / images / media / 319

What I want is, I want to verify the file type supported(that is jpg/jpeg) and the actual filename. 我想要的是,我想验证支持的文件类型(即jpg / jpeg)和实际文件名。

Is there a way to get the Filename with extension, which is selected. 有没有办法获得带有扩展名的文件名,该文件名已被选中。

Thanks in advance. 提前致谢。

Code Snippet: 代码片段:

        var data = {};
        if( type === CAMERA){
            data = {
                        quality: quality,
                        destinationType: FILE_URI,
                        encodingType: JPEG, targetWidth: 1200, targetHeight: 1200,
                        saveToPhotoAlbum: true
                    };
        }
        else
        {
            data = {
                        destinationType: FILE_URI,
                        sourceType: PHOTOLIBRARY,
                        mediaType: ALLMEDIA

                    };
        }


        navigator.camera.getPicture(
                successCallback, errorCallback, data
        );   

      //The success callback method is : 
       successCallback: function(imageURI, param)
       {
                 //HERE THE imageURI value is coming with different format if selected from GALLERY
                 window.resolveLocalFileSystemURI(imageURI, 
            function(fileEntry) {fileEntry.file(onSuccess,onError);},
                            function(evt) {onError.call(this,evt.target.error);} );

       }

I was able to convert from a "content://" URI to a "file://" URI using this plugin: https://www.npmjs.com/package/cordova-plugin-filepath . 我能够使用此插件从“content://”URI转换为“file://”URI: https//www.npmjs.com/package/cordova-plugin-filepath

After obtaining the "file://" URI, I'm then able to use Cordova's resolveLocalFileSystemURL() function. 获得“file://”URI后,我就可以使用Cordova的resolveLocalFileSystemURL()函数了。

Hope this helps. 希望这可以帮助。

if (fileUri.startsWith("content://")) {
    //We have a native file path (usually returned when a user gets a file from their Android gallery)
    //Let's convert to a fileUri that we can consume properly
    window.FilePath.resolveNativePath(fileUri, function(localFileUri) {
        window.resolveLocalFileSystemURL("file://" + localFileUri, function(fileEntry) {/*Do Something*/});
    });
}

in phonegap getpicture method navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] ); 在phonegap getpicture方法navigator.camera.getPicture(cameraSuccess,cameraError,[cameraOptions]);

we can give cameraOptions 我们可以提供cameraOptions

{ quality : 75,
  destinationType : Camera.DestinationType.DATA_URL,
  sourceType : Camera.PictureSourceType.CAMERA,
  allowEdit : true,
  encodingType: Camera.EncodingType.JPEG,
  targetWidth: 100,
  targetHeight: 100,
  popoverOptions: CameraPopoverOptions,
  saveToPhotoAlbum: false };

check the options first. 首先检查选项。

Android Quirks Android Quirks

Android 4.4 only: Android 4.4 introduced a new Storage Access Framework that makes it easier for users to browse and open documents across all of their preferred document storage providers. 仅限Android 4.4:Android 4.4引入了新的存储访问框架,使用户可以更轻松地浏览和打开所有首选文档存储提供程序中的文档。 Cordova has not yet been fully integrated with this new Storage Access Framework. Cordova尚未与这种新的存储访问框架完全集成。 Because of this, the getPicture() method will not correctly return pictures when the user selects from the "Recent", "Drive", "Images", or "External Storage" folders when the destinationType is FILE_URI. 因此,当destinationType为FILE_URI时,当用户从“Recent”,“Drive”,“Images”或“External Storage”文件夹中进行选择时,getPicture()方法将无法正确返回图片。 However, the user will be able to correctly select any pictures if they go through the "Gallery" app first. 但是,如果用户首先浏览“图库”应用程序,则可以正确选择任何图片。 Potential workarounds for this issue are documented on this StackOverflow question . StackOverflow问题记录了此问题的潜在解决方法。 Please see CB-5398 to track this issue. 请参阅CB-5398以跟踪此问题。

Android uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. Android使用意图在设备上启动摄像头活动以捕获图像,而在内存较低的手机上,Cordova活动可能会被杀死。 In this scenario, the image may not appear when the Cordova activity is restored. 在这种情况下,恢复Cordova活动时可能不会显示图像。

If you got fileEntry you can use file() method to get mettadatas 如果你有fileEntry,你可以使用file()方法获取mettadatas

 function cameraSuccess(urls) {

       window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(){

                    // alert('success requestFileSystem');

                 }, function(){
                 //error

                   });

        window.resolveLocalFileSystemURI(urls, function(fileEntry){


                   fileEntry.file(function(file){

                             // alert(JSON.stringify(file)); //view full metadata
                                var type = file.type;
                                var nameoffile = file.name;

                               }, function(){

                                //error                                                 
                                });

                  },function(){

                 // error 
                  } ); 

One Last option is to create one custom plugin for finding type of image 最后一个选项是创建一个自定义插件来查找图像类型

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

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