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

时间:2014-05-27 04:09:19

标签: javascript android jquery-mobile cordova

使用 phonegap camera API 时,如何在Camera.DestinationType.FILE_URI.中同时选择或选择多张图片我一次只能选择一张图片。我可以使用 this在sdcard中选择多个文件(包括txt,pdf ..)。所以我想要像图像一样。

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

我的cordova版本3.3,Jquery Mobile 1.3.2。

请建议任何插件都可以执行此操作。

3 个答案:

答案 0 :(得分:2)

使用此Cordova multiple image selector plugin一次选择多张图片。这是选择多个图像的好插件。

下载以上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

答案 1 :(得分:0)

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是从相机和照相馆中进行多重图像的循环

答案 2 :(得分:0)

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