android - 使用 phonegap (navigator.camera.getPicture) 拍照后,如何将其存储在本地驱动器中?

标签 android cordova

这是我的 phonegap 代码。此代码用于拍照以及保存图片:

<!DOCTYPE html>
<html>
<head>
<title>Capture Photo and Save</title>

<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">
    var pictureSource; // picture source
    var destinationType; // sets the format of returned value 

    // Wait for Cordova to connect with the device
    document.addEventListener("deviceready", onDeviceReady, false);

    // Cordova 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) {
        // Uncomment to view the base64 encoded image data
        var date = ""
        var d = new Date();
        date = "" + d.getDate() + "-" + (d.getMonth() + 1) + "-"
                + d.getFullYear();
        alert(date)
        //alert(imageData);  

        // Get image handle
        //
        var smallImage = document.getElementById('smallImage');
        // Unhide image elements
        //
        smallImage.style.display = 'block';

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        smallImage.src = "data:image/jpeg;base64," + imageData;

        alert("data:image/jpeg;base64," + imageData)

        //This part is for saving the capture photo
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

        function gotFS(fileSystem) {
            alert("image/" + date + ".jpeg")
            fileSystem.root.getFile("image/" + date + ".jpeg", {
                create : true,
                exclusive : false
            }, gotFileEntry, fail);
        }
        function gotFileEntry(fileEntry) {
            fileEntry.createWriter(gotFileWriter, fail);
        }
        function gotFileWriter(writer) {
            var data = "data:image/jpeg;base64," + imageData;
            writer.write(data);

        }
        function fail(error) {
            alert("error")
            console.log(error.code);
        }
    }

    // Called when a photo is successfully retrieved
    function onPhotoURISuccess(imageURI) {
        // Uncomment to view the image file URI 
        // console.log(imageURI);

        // Get image handle
        var largeImage = document.getElementById('largeImage');
        // Unhide image elements
        largeImage.style.display = 'block';

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        largeImage.src = imageURI;
    }

    // A button will call this function
    function capturePhoto() {
        // Take picture using device camera and retrieve image as base64-encoded string
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
            quality : 50,
            destinationType : destinationType.DATA_URL
        });
    }

    // A button will call this function
    //
    function capturePhotoEdit() {
        // Take picture using device camera, allow edit, and retrieve image as base64-encoded string  
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
            quality : 20,
            allowEdit : true,
            destinationType : destinationType.DATA_URL
        });
    }

    // A button will call this function
    function getPhoto(source) {
        // 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);
    }
    function savePhoto(source) {

    }
</script>
</head>
<body>
    <button onclick="capturePhoto();">Capture Photo</button>
    <br>
    <button onclick="capturePhotoEdit();">Capture Editable Photo</button>
    <br>
    <button onclick="savePhoto();">Save The Photo In Server</button>
    <br>
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From
        Photo Library</button>
    <br>
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From
        PhotoAlbum</button>
    <br>
    <img style="display: none; width: 60px; height: 60px;" id="smallImage"
        src="" />
    <img style="display: none;" id="largeImage" src="" />
</body>
</html>

我正在拍照并尝试像这样保存它但它保存为文件而不是图片(二进制)。所以在保存图片后它没有打开。我如何用适当的 fromat 保存它以便我可以用图片打开吗?

最佳答案

我是这样做的:

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

function onPhotoDataSuccess(imageURI) {
  var gotFileEntry = function(fileEntry) {
    //alert("got image file entry: " + fileEntry.fullPath);
    var gotFileSystem = function(fileSystem) {

        fileSystem.root.getDirectory("MyAppFolder", {
            create : true
        }, function(dataDir) {
          var d = new Date();
          var n = d.getTime();
          //new file name
          var newFileName = n + ".jpg";
console.log("Saved image" + dataDir);
            // copy the file
            fileEntry.moveTo(dataDir, newFileName, null, fsFail);

        }, dirFail);

    };
    // get file system to copy or move image file to
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem,
        fsFail);
};
// resolve file system for image
window.resolveLocalFileSystemURI(imageURI, gotFileEntry, fsFail);

// file system fail
var fsFail = function(error) {
    alert("failed with error code: " + error.code);

};

var dirFail = function(error) {
    alert("Directory error code: " + error.code);

};
}

希望对你有帮助!

关于android - 使用 phonegap (navigator.camera.getPicture) 拍照后,如何将其存储在本地驱动器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16189683/

相关文章:

android - 将 Paging 3 alpha 更新为稳定导致索引问题 Android

java - 如何在android中调用另一个类的方法?

cordova - 如何将 Linkedin 与 PhoneGap 连接

android - 在 Cordova v-5.0 CLI 中签名 APK 文件不起作用

javascript - cordova windows 8应用程序blob url到ms-appdata url

android - 散列 (#) 从已发送的 USSD 中删除

Android -- 检索默认系统字体字符串名称

android - 如何根据城市自定义应用程序

cordova - 无法在 Cordova 上加载 Stripe 3ds iframe

android - Phonegap Android 无法呈现 CSS 的蒙版图像,但适用于 iOS