我目前正在使用Phonegap/Cordova和jQuerymobile构建适用于iOS的应用程序。这个想法是用相机拍照并存储捕获的图像以备将来使用。我想将路径/文件名存储到我的本地数据库中,并将图片文件移动到iPhone中的持久位置。

有人可以给我一个例子吗?

最佳答案

好的,这是解决方案。

HTML文件中的

  • 我有一个图像标签,用于显示相机拍摄的照片:
  • 我有一个运行拍照功能的按钮:
    拍摄照片
  • 捕获照片的功能是(拍摄照片时,将使用照片的路径填充“smallImage” ID的scr)
    function capturePhoto() {
    // Take picture using device camera and retrieve image as base64-encoded string
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
    }
    
    //Callback function when the picture has been successfully taken
    function onPhotoDataSuccess(imageData) {
        // Get image handle
        var smallImage = document.getElementById('smallImage');
    
        // Unhide image elements
        smallImage.style.display = 'block';
        smallImage.src = imageData;
    }
    
    //Callback function when the picture has not been successfully taken
    function onFail(message) {
        alert('Failed to load picture because: ' + message);
    }
    
  • 现在,我想将图片移到一个永久文件夹中,然后将链接保存到我的数据库中:
    function movePic(file){
        window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError);
    }
    
    //Callback function when the file system uri has been resolved
    function resolveOnSuccess(entry){
        var d = new Date();
        var n = d.getTime();
        //new file name
        var newFileName = n + ".jpg";
        var myFolderApp = "EasyPacking";
    
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
        //The folder is created if doesn't exist
        fileSys.root.getDirectory( myFolderApp,
                        {create:true, exclusive: false},
                        function(directory) {
                            entry.moveTo(directory, newFileName,  successMove, resOnError);
                        },
                        resOnError);
                        },
        resOnError);
    }
    
    //Callback function when the file has been moved successfully - inserting the complete path
    function successMove(entry) {
        //I do my insert with "entry.fullPath" as for the path
    }
    
    function resOnError(error) {
        alert(error.code);
    }
    
  • 我的文件已保存在数据库中显示出来,我在包含图像src的行的最前面放了“file://”

  • 希望对您有所帮助。
    J.

    P.S. :
    -非常感谢Simon Mac Donald(http://hi.im/simonmacdonald)在googledocs上的帖子。

    10-04 22:47
    查看更多