如何在phonegap中同时使用相机API挑选(选择)多个图像

How to pick(choose) multiple images using camera API at the same time in phonegap?

本文关键字:挑选 API 选择 图像 相机 phonegap      更新时间:2023-09-26

当使用 Camera.DestinationType.FILE_URI. 时,如何在phonegap camera API中同时选择或选择多个图像,我一次只能选择一个图像。我能够选择多个文件(包括txt,pdf..)在sd卡使用这个。我想要和图像一样。

navigator.camera.getPicture(function(imageData) {
window.resolveLocalFileSystemURI(imageData, function(fileEntry) {
            fileEntry.file(function(fileObj) {
                    }, onFail, {
    quality : 50,
    destinationType : Camera.DestinationType.FILE_URI
});

My cordova version 3.3, Jquery Mobile 1.3.2.

请建议任何插件可用来做到这一点。

使用这个Cordova多图像选择器插件一次选择多个图像。这是一个很好的插件选择多个图像。

下载以上plugin and copy paste the java classes。设置所需的permission。别忘了把res folder复制粘贴到你的res文件夹里。

assets/www中创建imagepicker.js复制并粘贴下载的imagepicker.js

在您的index.html设置如下:

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="imagepicker.js"></script>
<script type="text/javascript">
    document.addEventListener("deviceready",onDeviceReady,false);
    function onDeviceReady(){
        window.imagePicker.getPictures(
                function(results) {
                    for (var i = 0; i < results.length; i++) {
                        alert('Image URI: ' + results[i]);
// read file type and size and file name like below(in comment)
/* window.resolveLocalFileSystemURI(results[i], function(fileEntry){
        fileEntry.file(function(fileObj) { 
            alert(fileEntry.name);
            alert(fileObj.size);
            alert(fileObj.type);
        }); 
    }, function (error) {
            alert('Error: ' + error);
        });*/
                    }
                }, function (error) {
                    alert('Error: ' + error);
                }
            );
    }
    </script>

Note: This should work only cordova 3.0 and above and android 4.0 and above

打开CameraLauncher.java文件,替换以下行

String resizePath = getTempDirectoryPath() + "/resize.jpg";
this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());

String resizePath = getTempDirectoryPath() + "/resize"+System.currentTimeMillis()+".jpg";
this.callbackContext.success("file://" + resizePath);

var x=0;
function onPhotoDataSuccess(imageURI)
{
    x++;
    // Uncomment to view the base64-encoded image data
    console.log(imageURI);
	alert(imageURI);
    // Get image handle
    //
    var y = 'smallImage'+x;
    var smallImage = document.getElementById(y);
    alert(smallImage);
	smallImage.src = "data:image/jpeg;base64," + imageURI;
    // Unhide image elements
    //
    smallImage.style.display = 'block';
  
    // Show the captured photo
    // The in-line CSS rules are used to resize the image
    //
    //var fso=new ActiveXObject("Scripting.FileSystemObject");
    //fso.CopyFile("data:image/jpeg;base64," + imageURI,"file:///storage/sdcard/DCIM/");
      
    alert(smallImage.src)
}

其中x是循环用于从camera和photogallery中获取多个图像