使用PhoneGap从设备图库中选择多张照片

6

我能够基于PhoneGap文档中camera.getPicture的完整示例构建一个测试应用程序。它允许我拍照或从相册中检索照片并将其放置在一个div中。

然而,我希望能够从相册中选择多个图像,并将每个图像放置在自己的div中。有谁能指点我如何实现这个目标?

谢谢。

这是我正在使用的javascript:

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;
}

// Called when a photo is successfully retrieved
function onPhotoDataSuccess(imageData) {
  var largeImage = document.getElementById('largeImage');
  largeImage.style.display = 'block';
  largeImage.src = "data:image/jpeg;base64," + imageData;
}


function onPhotoURISuccess(imageURI) {
  var largeImage = document.getElementById('largeImage');
  largeImage.style.display = 'block';
  largeImage.src = imageURI;
}

// A button will call this function
function capturePhoto() {
    //add new div

    var newPhoto = document.createElement("div");
    newPhoto.id = "div";        
    newPhoto.className ="photo";
    newPhoto.innerHTML = "<img id='largeImage' src='' />";
    document.getElementById("photos").appendChild(newPhoto);


  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onPhotoURISuccess, onFail, { quality: 50 });
}

// A button will call this function
function getPhoto(source) {
    //add new div



  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}


// Called if something bad happens.
function onFail(message) {
  alert('Failed because: ' + message);

1
我也在寻找同样的答案 - 你最终解决了这个问题吗? - woolyninja
2个回答

2

-1
你需要在每张照片拍摄后动态创建div。 你的成功回调函数应该像这样:
function onPhotoDataSuccess(imageData) {
    // the following is all one line.
    document.getElementById("photos").innerHTML+=
    "<div>\
         <img src=\"data:image/jpeg;base64,"+imageData+"\">\
     </div>";
}

然后你可以使用类似于以下的 CSS 样式表来设置所有图像的样式:

#photos > div {
    width: 100px;
    margin:10px;
    float:left;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接