2013-02-18 59 views
14

我對phonegap非常陌生,它表示它具有捕獲功能。所以我用它,非常好。然而,我在html中顯示圖片,但我不知道如何保存圖像。根據http://docs.phonegap.com/en/1.7.0/cordova_camera_camera.md.html將圖像保存在本地存儲PhonePhone

你可以做任何你想用的編碼圖像或URI

,例如:

呈現在標籤圖像(見下面的例子) 保存在本地的數據(localStorage的,Lawnchair等) 後的數據傳送到遠程服務器

不幸的是在如何做到這一點

沒有示例代碼

如何將圖像保存在LocalStorage或設備圖庫中?

+1

發現https://github.com/raananw/PhoneGap-Image-Resizer這是否仍然有效? – jhdj 2013-02-21 05:45:34

+0

它工作只需要一些調整 – jhdj 2013-02-26 02:59:54

回答

22

偉大的你找到了解決方案,我做了以下幾點。 希望它可以幫助別人。

只需調用按鈕單擊事件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以便將其插入到本地數據庫中。

+2

這不適合我。你有工作副本嗎? – surhidamatya 2013-09-23 10:06:29

+0

感謝它爲我工作。:) – mohitum 2014-01-16 12:41:21

+0

我可以捕捉的圖像,但未能將其保存到本地存儲 – 2015-04-23 11:08:13

9

在選項設置爲True著作saveToPhotoAlbum很好的爲好。從2.9文檔here得到。

+1

來自文檔的簡單建議的額外要點。 – leech 2013-12-19 20:12:09