Cordova文件管理,这是正确的做法

时间:2014-09-24 13:11:45

标签: javascript cordova

我正在使用Phonegap制作应用程序,用户可以在其中选择和共享文件。当用户选择它即使离线或关闭应用程序我应该将其上传回服务器,所以我需要存储参考文件路径。以下是我使用的步骤和代码

  1. 用户使用相机选择/捕捉视频或图像。
  2. 文件可能不包含每次Android中名称的扩展名,在iOS中从Gallery中选择。
  3. 因此,获取mimeType,然后使用新名称和扩展名将文件复制到myApp的文件夹。
  4. 然后再次获取mimeType并返回。
  5. 但我的问题是,无论何时用户选择媒体文件,复制文件和其他位置是否正确。

    代码

     var obj = {
        destinationType: navigator.camera.DestinationType[fromCamera ? 'FILE_URI' : 'NATIVE_URI'],
        // If I use NATIVE_URI for when taking from Camera it returns null.
        sourceType: navigator.camera.PictureSourceType[fromCamera ? 'CAMERA' : 'PHOTOLIBRARY'],
        mediaType: navigator.camera.MediaType[image ? 'PICTURE' : 'VIDEO']
    }, that = this;
    
    if (image && fromCamera) {
        obj.correctOrientation = true;
        obj.quality = 40;
    }
    
    return new Promise(function (resolve, reject) {
        navigator.camera.getPicture(function (url) {
            var fail = function () {
                resolve(false);
            }
            window.setTimeout(function () {
                // mimeType uses window.resolveLocalFileSystemURL to get fileEntry
                that.mimeType(url).then(function (mimeObj) {
                    /* 
                        mimeObj contains object like below  
                        {lastModified:'', fileType:'', fileExtension:'', name : ''}
                    */
                    if (mimeObj) {
                        // that.fileSystem() resolves fileSystem
                        that.fileSystem().then(function (fs) {
                            // dirName is where I want to store all myApp's media files.
                            var dirName = 'myApp/media/' + (image ? "images" : "videos") + '/';
                            that.getFile(url).then(function (fileEntry) {
                                // getFile uses window.resolveLocalFileSystemURL and returns fileEntry
                                that.moveTo(fs, fileEntry, dirName, mimeObj.name).then(function (entry) {
                                    // MoveTo fill copy the same file to directory where I am manintaing files.
                                    mimeObj.localURL = entry.nativeURL;
                                    /* 
                                        mimeObj is failing to find extension 
                                        when user selects it from different albums. But not when user uses Camera. 
                                        So I am making again mimeType call.
                                    */
                                    that.mimeType(mimeObj.localURL).then(function (mimeObj) {
                                        // This gives me exact mimeObj that I need.
                                        resolve(mimeObj);
                                    });
                                }, fail);
                            }, fail);
                        }, fail);
                    } else {
                        resolve(false);
                    }
                });
            }, 0);
    
        }, function (fail_message) {
            window.setTimeout(function () {
                reject(fail_message);
            }, 0);
        }, obj);
    });
    

0 个答案:

没有答案