使用phonegap从相机或图库中选择图像

时间:2012-09-01 01:37:48

标签: android cordova jquery-mobile

<script type="text/javascript" charset="utf-8">



var pictureSource;   // Picture source

var destinationType; // Sets the format of returned value 

// Wait for PhoneGap to connect with the device


document.addEventListener("deviceready", onDeviceReady, false);


// PhoneGap is ready to be used!

function onDeviceReady() 

{


   pictureSource = navigator.camera.PictureSourceType;

    destinationType = navigator.camera.DestinationType;


}

function capturePhoto() {

    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 25, destinationType: 
Camera.DestinationType.FILE_URI });

}

function onPhotoURISuccess(imageURI) {

    createFileEntry(imageURI);
}

function createFileEntry(imageURI) {

    window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);    
}

function copyPhoto(fileEntry) {

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) { 
        fileSys.root.getDirectory("photos", {create: true, exclusive: false}, 

function(dir) { 

  fileEntry.copyTo(dir, "file.jpg", onCopySuccess, fail); 

            }, fail); 
    }, fail); 
}

function onCopySuccess(entry) {

    console.log(entry.fullPath)
}

function fail(error) {

    console.log(error.code);
}


    </script>

1 个答案:

答案 0 :(得分:2)

您应该使用PhoneGap 2.0.0 camera object。该文档提供了完整的照片捕获示例。

此外,navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );使用相机拍摄照片或从设备的相册中检索照片。图像作为base64编码的String或图像文件的URI返回。

我希望这会有所帮助。