15

我对phonegap很陌生,它说它具有捕获功能。所以我使用它并且非常好。但是我在 html 中显示了图片,但我不知道如何保存图片。

根据http://docs.phonegap.com/en/1.7.0/cordova_camera_camera.md.html

您可以对编码的图像或 URI 做任何您想做的事情,例如:

在标签中渲染图像(参见下面的示例) 在本地保存数据(LocalStorage、Lawnchair 等) 将数据发布到远程服务器

不幸的是,没有关于如何做到这一点的示例代码

如何将图像保存在 LocalStorage 或设备库中?

4

2 回答 2

23

太好了,您找到了解决方案,我按照以下方式进行了操作。希望它可以帮助别人。

只需在按钮单击事件上调用capturePhoto函数。

// A button will call this function
//
function capturePhoto() {
    sessionStorage.removeItem('imagepath');
    // Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}

function onPhotoDataSuccess(imageURI) { 
        // Uncomment to view the base64 encoded image data
        // console.log(imageData);

        // Get image handle
        //
        var imgProfile = document.getElementById('imgProfile');

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        imgProfile.src = imageURI;
        if(sessionStorage.isprofileimage==1){
            getLocation();
        }
        movePic(imageURI);
}

// Called if something bad happens.
// 
function onFail(message) {
    alert('Failed 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 = "MyAppFolder";

    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) {
    //Store imagepath in session for future use
    // like to store it in database
    sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError(error) {
    alert(error.code);
}

这段代码的作用是

捕获图像并将其存储在设备 SD 卡上的MyAppFolder中。并将图像路径存储在会话中,以便将其插入本地数据库。

于 2013-05-20T11:46:38.120 回答
9

将选项中的 saveToPhotoAlbum 设置为 True 也很有效。从这里的 2.9 文档中得到。

于 2013-09-10T18:27:15.843 回答