【问题标题】:How would I get a File object from PhoneGap camera.getPicture?我如何从 PhoneGap camera.getPicture 获取 File 对象?
【发布时间】:2014-03-03 17:46:00
【问题描述】:

这可能很简单,PhoneGap 的“Camera”插件、“File”插件或“File-Transfer”插件中的一些功能组合涵盖了这一点。我知道用户可以选择一个文件:

navigator.camera.getPicture(function (fileURI) {

    // *** need help here ***

}, function ()
    // handle errors
}, {
    destinationType: window.Camera.DestinationType.FILE_URI,
    sourceType: window.Camera.PictureSourceType.PHOTOLIBRARY,
    mediaType: window.Camera.MediaType.ALLMEDIA
});

如果有区别,我也可以更改为 destinationType: window.Camera.DestinationType.DATA_URL

我在成功处理程序中的目标是获取一个 File 对象 (https://developer.mozilla.org/en-US/docs/Web/API/File)。

【问题讨论】:

    标签: javascript android ios cordova


    【解决方案1】:

    应该这样做。

    navigator.camera.getPicture(function (fileURI) {
    
        window.resolveLocalFileSystemURL(fileURI, 
            function(fileEntry){
                alert("got image file entry: " + fileEntry.fullPath);
                // fileEntry.file() should return a raw HTML File Object
            },
            function(){//error}
        );
    
    }, function (){
    // handle errors
    }, {
        destinationType: window.Camera.DestinationType.FILE_URI,
        sourceType: window.Camera.PictureSourceType.PHOTOLIBRARY,
        mediaType: window.Camera.MediaType.ALLMEDIA
    });
    

    【讨论】:

    • 对于其他希望执行此操作的其他人的答案的补充:Cordova FileEntry 对象具有返回 javascript File 对象的 file() 方法。
    【解决方案2】:
    window.resolveLocalFileSystemURI(fileURI, function(fileEntry) { /* your code here */ });
    

    【讨论】:

    • resolveLocalFileSystemURI 已弃用。请改为调用 resolveLocalFileSystemURL。
    • 我收到了这个Uncaught TypeError: window.resolveLocalFileSystemURL is not a function的错误
    • 我的代码如波涛navigator.camera.getPicture((imgURI) => { window.resolveLocalFileSystemURL(imgURI, (fileUri) => { const currentUpload = new Upload(fileUri.file()); this.appConfigService.pushUpload(currentUpload, this.esCode); document.getElementById('msg').textContent = imgURI; }, err => { console.log(err, 'navigator.camera.getPicture3'); }); }, this.uploadfai, opts);
    猜你喜欢
    • 2018-11-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多