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

Jan*_*joy 5 android jquery-mobile cordova

<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>
Run Code Online (Sandbox Code Playgroud)

Apo*_*dis 2

您应该使用PhoneGap 2.0.0 相机对象。该文档提供了完整的照片捕获示例。

此外,还navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );可以使用相机拍照或从设备相册中检索照片。图像以 base64 编码字符串或图像文件的 URI 形式返回。

我希望这有帮助。