2012-03-28 8 views
2

次のコードを使用してimage base64 dataを表示してサーバーにアップロードしています。 しかし、私はこのキャプチャされたイメージをsdcardフォルダに保存します。これを手伝ってください。PhoneGapでキャプチャした画像をsdcardのフォルダに移動する方法

サーバーがこの形式のみをサポートしているため、これはBase64イメージデータを取得するために必要です。だから私はdestinationType = 0(手段DATA_URL)を使用しています。私は今base64の画像データを持っていますが、どうすればこのデータをsdcardに保存できますか?あなたはBase64形式でデータを持っているので

uploadPhoto(isSourceCamera, onPhotoDataSuccess, onFail); 

function uploadPhoto(isSourceCamera, onPhotoDataSuccess, onFail) 
{ 
    pictureSource = navigator.camera.PictureSourceType; 
    if (isSourceCamera) 
    { 
     //QUALITY MUST BE LOW TO AVOID MEMORY ISSUES ON IPHONE4 ! (and other low memory phones). 
     //must resize to make it faster to upload and avoid failure with low memory phones. 
     navigator.camera.getPicture(onSuccessFunc, onFail, { 
          quality : 35, 
          sourceType : pictureSource.CAMERA, 
          targetWidth:750, 
          targetHeight:750, 
          allowEdit:true, 
          destinationType:0 
          }); 
    } 
    else 
    { 
     navigator.camera.getPicture(onSuccessFunc, onFail, { 
          quality : 35, 
          sourceType : pictureSource.PHOTOLIBRARY, 
          targetWidth:750, 
          targetHeight:750, 
          allowEdit:true, 
          destinationType:0 
          }); 
    } 
} 

function onPhotoDataSuccess(imageData) 
{ 
    **//I want to smae my image here in sdcard folder.** 
    var nodeid = localStorage.getItem("user_nodeid"); 
    var modifyImgData = imageData.replace(' ', '+'); 
    document.getElementById('image').src = setLocalStorageImage(localStorage.getItem(nodeid+"image"), modifyImgData); 
    document.getElementById('profileMenu').src = setLocalStorageImage(localStorage.getItem(nodeid+"smallimage"), modifyImgData); 
    $.ajax({ 
      type: "POST", 
      url: appURL+"api/upload/image/" +nodeid+ "/1", 
      data: "image=" + encodeURIComponent(modifyImgData), 
      success: function(msg){ 
        //No need to do anything here 
        if (msg.documentElement.getElementsByTagName("message")[0].childNodes[0].nodeValue != 'success') 
         onFail('Error in uploading image at server. Please try again.'); 
      } 
     }); 
} 

function onFail(message){ 
    alert(message); 
} 

答えて

0

あなただけのディスクにデータを保存するためにFileWriterを使用することができます。

+0

こんにちはサイモンとして返信用 感謝をbase64でDATA_URLをretriveことができ、私は "てFileWriter" を試してみましたが、SDカード(SDカード/ myphotosのような)のperticularの場所に保存することsuccessedありません。 このパスを割り当てる方法は? また、base64データをphonegapの有効なイメージとして保存する方法がわかりませんか? 助けてください。 – Ram

+0

ここに、FileWriterを使用する良い例があります。 http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileWriter_full_example –

2

元の質問に対する正しい答えは次のとおりです。 PhoneGapでキャプチャしたイメージをsdcardのフォルダに移動するにはどうすればよいですか?

function onfail(error,caller){ 
    error = error || '[error]'; 
    caller = caller || '[caller]'; 
    alert('Error > '+caller+" code: "+error.code); 
}; 

/* 
    Error codes 
    NOT_FOUND_ERR = 1; 
    SECURITY_ERR = 2; 
    ABORT_ERR = 3; 
    NOT_READABLE_ERR = 4; 
    ENCODING_ERR = 5; 
    NO_MODIFICATION_ALLOWED_ERR = 6; 
    INVALID_STATE_ERR = 7; 
    SYNTAX_ERR = 8; 
    INVALID_MODIFICATION_ERR = 9; 
    QUOTA_EXCEEDED_ERR = 10; 
    TYPE_MISMATCH_ERR = 11; 
    PATH_EXISTS_ERR = 12; 
*/ 


function doCameraAPI() { 
    // Retrieve image file location from specified source 
    navigator.camera.getPicture(getImageURI, function(message) { 
     alert('Image Capture Failed'); 
    }, { 
     quality : 40, 
     destinationType : Camera.DestinationType.FILE_URI 
    }); 

}; //doCameraAPI 

function getImageURI(imageURI) { 

    //resolve file system for image to move. 
    window.resolveLocalFileSystemURI(imageURI, gotFileEntry, function(error){onfail(error,'Get Target Image')}); 


    function gotFileEntry(targetImg) { 
     //alert("got image file entry: " + targetImg.name);  
     //now lets resolve the location of the destination folder 
     window.resolveLocalFileSystemURI(POSTPATH, gotDestinationEntry, function(error){onfail(error,'Get Destination Dir')}); 
     function gotDestinationEntry(destination){ 
      // move the file 
      targetImg.moveTo(destination, targetImg.name, moveSuccess, function(error){onfail(error,'Move Image')}); 
       alert('dest :'+destination.fullPath); 
      }; 

    function moveSuccess(){ 
     alert('FILE MOVE SUCCESSFUL!'); 
    }; 
}; //getImageURI 

:google phonegapグループのnbk

+0

ちょっと複雑ですね。なぜそれは,URIとtoURIの2つのパラメータを持つメソッドとして実装できないのですか?私はあなたの解決策を選んでいません、なぜそれが本当に可能かどうか、それをやり直す方が良いのではないかと疑問に思っていますか? –

+1

window.resolveLocalFileSystemURI()でエラーが発生します。 –

0

私はFILE_URLとしてイメージをキャプチャし、最初に上記のSteven Benjaminが述べたようにイメージをsdcardに保存する必要があると思います。

次にあなたが

function readFile() { // button onclick function 
    var gotFileEntry = function(fileEntry) { 
     console.log("got image file entry: " + fileEntry.fullPath); 
     fileEntry.file(function(file) { 
      var reader = new FileReader(); 
      reader.onloadend = function(evt) { 
       console.log("Read complete!"); 
       image64.value = evt.target.result; 
      }; 
      reader.readAsDataURL(file); 
     }, failFile); 
    }; 
    window.resolveLocalFileSystemURI("file:///mnt/sdcard/test.jpg", gotFileEntryImage, function(){console.log("* * * onPhotoURISuccess" + failed);}); 
} 
関連する問題