图像保存在本地存储的PhoneGap [英] Save image in local storage phonegap

查看:146
本文介绍了图像保存在本地存储的PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的PhoneGap的,它说,它有一个捕获功能。所以我用它,是非常好的。不过,我显示的图像的HTML,但我不知道如何保存图像。

I am quite new to phonegap and it says that it has a capture feature. So i used it and was very nice. However I displayed the picture in the html but i dont know how to save the image.

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

您可以做任何你想做的EN codeD映像或URI,例如:

You can do whatever you want with the encoded image or URI, for example:

渲染在标签图像(见下面的例子)   保存数据在本地(localStorage的,Lawnchair等)   帐数据到远程服务器

Render the image in an tag (see example below) Save the data locally (LocalStorage, Lawnchair, etc) Post the data to a remote server

不幸的是在如何做到这一点没有样品code

Unfortunately there was no sample code on how to do it

我如何保存图像的localStorage或设备的画廊?

How do i save image in LocalStorage or gallery of device?

推荐答案

大,你找到了解决办法,我做到了下面的方法。 希望它可以帮助别人。

Great you found the solution, I did it the following way. Hope it helps someone else.

要获取的 capturePhoto 按钮单击事件的功能。

Simply call capturePhoto function on button click event.

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

这是什么code的作用是

What this code does is

捕捉图像,并将其存储在 MyAppFolder 在设备的SD卡。 并存储的ImagePath 的会议,以便将其插入本地数据库。

Captures image and stores it in MyAppFolder on device's SD Card. And stores imagepath in session so as to insert it in local database.

这篇关于图像保存在本地存储的PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆