android - 将图像保存在本地存储phonegap中

标签 android image cordova camera local-storage

我对 phonegap 很陌生,它说它具有捕获功能。所以我使用它并且非常好。但是我在html中显示了图片,但我不知道如何保存图片。

根据 http://docs.phonegap.com/en/1.7.0/cordova_camera_camera.md.html

你可以对编码后的图像或 URI 做任何你想做的事情,例如:

在标签中渲染图像(参见下面的示例) 在本地保存数据(LocalStorage、Lawnchair 等) 将数据发布到远程服务器

很遗憾,没有关于如何操作的示例代码

如何将图像保存在本地存储或设备图库中?

最佳答案

太好了,您找到了解决方案,我是按照以下方式完成的。 希望对其他人有所帮助。

只需在按钮点击事件上调用 capturePhoto 函数。

// A button will call this function
//
function capturePhoto() {
    sessionStorage.removeItem('imagepath');
    // 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) { 
        // Uncomment to view the base64 encoded image data
        // console.log(imageData);

        // Get image handle
        //
        var imgProfile = document.getElementById('imgProfile');

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        imgProfile.src = imageURI;
        if(sessionStorage.isprofileimage==1){
            getLocation();
        }
        movePic(imageURI);
}

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

function movePic(file){ 
    window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); 
} 

//Callback function when the file system uri has been resolved
function resolveOnSuccess(entry){ 
    var d = new Date();
    var n = d.getTime();
    //new file name
    var newFileName = n + ".jpg";
    var myFolderApp = "MyAppFolder";

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {      
    //The folder is created if doesn't exist
    fileSys.root.getDirectory( myFolderApp,
                    {create:true, exclusive: false},
                    function(directory) {
                        entry.moveTo(directory, newFileName,  successMove, resOnError);
                    },
                    resOnError);
                    },
    resOnError);
}

//Callback function when the file has been moved successfully - inserting the complete path
function successMove(entry) {
    //Store imagepath in session for future use
    // like to store it in database
    sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError(error) {
    alert(error.code);
}

这段代码的作用是

捕获图像并将其存储在设备 SD 卡上的 MyAppFolder 中。 并将 imagepath 存储在 session 中,以便插入到本地数据库中。

关于android - 将图像保存在本地存储phonegap中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928202/

相关文章:

cordova - iOS7 启动画面(启动画面)在底部留一个空格

android - android程序如何删除刚拍的照片

c - 在 gtk 窗口中流式传输 libuvc 代码的过程是什么?

ios - 无法使用 plist 文件获取不同的图钉图像

javascript - 谷歌地图与 Phonegap 的集成

javascript - 你如何从 Ubuntu 中完全删除 Ionic 和 Cordova 安装?

android - 为什么我的接收器收不到广播?

android - 在activity中插入两个activity时出错

回调完成后Android完成 Activity

c# - 不同的内置图像抽象的优点是什么?