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

发布于 2024-12-03 17:27:29 字数 1917 浏览 2 评论 0原文

我能够基于 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);

I was able to build a test app based on the camera.getPicture full example in PhoneGap's documentation. It allows me to take a photo or retrieve a photo from the gallery and place it in a div.

However, I want to be able to select multiple images from the gallery and place each in its own div. Can anyone point me in the right direction to learn how to accomplish this?

Thanks.

Here is the javascript I'm using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-12-10 17:27:29

从 Phonegap 3.5 开始,不支持同时选择多个图像。您将需要编写或找到一个可以与本机代码配合使用的插件,以便您能够执行此操作。这是 Phonegap 开发计划中描述的问题。 https://issues.apache.org/jira/browse/CB-1215

我也在努力做这件事。这是 Android 解决方案的链接。

http://webcache.googleusercontent.com/search?q=cache:http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

As of Phonegap 3.5 there is no support for selecting multiple images at the same time. You will need to write or find a plugin that will work with the native code to enable you to do this. Here is the issue described in the Phonegap development plan. https://issues.apache.org/jira/browse/CB-1215

I am working on doing this as well. Here is a link for an Android solution.

http://webcache.googleusercontent.com/search?q=cache:http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

昔梦 2024-12-10 17:27:29

您需要在拍摄每张照片后动态创建 div。
你的成功回调将是这样的:

function onPhotoDataSuccess(imageData) {
    // the following is all one line.
    document.getElementById("photos").innerHTML+=
    "<div>\
         <img src=\"data:image/jpeg;base64,"+imageData+"\">\
     </div>";
}

然后你可以使用这样的东西通过CSS设置所有imgs的样式

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

you need to create the div dynamically after every photo is taken.
Your success callback would be something like this:

function onPhotoDataSuccess(imageData) {
    // the following is all one line.
    document.getElementById("photos").innerHTML+=
    "<div>\
         <img src=\"data:image/jpeg;base64,"+imageData+"\">\
     </div>";
}

then you can style all the imgs via css using something like this

#photos > div {
    width: 100px;
    margin:10px;
    float:left;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文