2013-02-18 39 views
14

Tôi khá mới đối với điện thoại và nói rằng nó có tính năng chụp. Vì vậy, tôi đã sử dụng nó và rất đẹp. Tuy nhiên tôi hiển thị hình ảnh trong html nhưng tôi không biết làm thế nào để lưu hình ảnh.Lưu hình ảnh vào bộ nhớ cục bộ phonegap

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

Bạn có thể làm bất cứ điều gì bạn muốn với hình ảnh mã hóa hoặc URI, ví dụ:

Render hình ảnh trong thẻ (xem ví dụ dưới đây) Lưu dữ liệu cục bộ (LocalStorage , Lawnchair, vv) Post dữ liệu đến một máy chủ từ xa

Đáng tiếc là không có mẫu mã trên như thế nào để làm điều đó

Làm cách nào để lưu hình ảnh trong LocalStorage hoặc bộ sưu tập thiết bị?

+1

tìm thấy https://github.com/raananw/PhoneGap-Image-Resizer thực hiện điều này vẫn hoạt động? – jhdj

+0

nó hoạt động chỉ cần một số chỉnh sửa – jhdj

Trả lời

22

Tuyệt vời khi bạn tìm thấy giải pháp, tôi đã làm theo cách sau. Hy vọng nó sẽ giúp người khác.

Chỉ cần gọi chụpPhoto chức năng trên sự kiện nhấp nút.

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

gì mã này không là

chụp ảnh và lưu trữ nó trong MyAppFolder trên thẻ SD của thiết bị. Và các cửa hàng imagepath trong phiên để chèn nó vào cơ sở dữ liệu cục bộ.

+2

Điều này không làm việc cho tôi. Bạn có một bản sao làm việc không. – surhidamatya

+0

Cảm ơn nó hoạt động cho tôi. :) – mohitum

+0

tôi có thể chụp ảnh nhưng không lưu được vào bộ nhớ cục bộ –

9

Đặt saveToPhotoAlbum trong tùy chọn thành True cũng hoạt động tốt. Nhận được từ 2.9 tài liệu here.

+1

Điểm bổ sung cho lời khuyên đơn giản từ tài liệu. – leech

Các vấn đề liên quan