2012-03-28 6 views
2

Ich verwende folgenden Code, um Bild base64 Daten zum Anzeigen und zum Server hochladen zu erhalten. Aber ich möchte dieses aufgenommene Bild im SDCard-Ordner speichern. Bitte hilf mir dabei.Wie aufgenommenes Bild in PhoneGap in einen Ordner in SD-Karte verschieben

Dies ist für mich notwendig, um base64-Bilddaten zu erhalten, da Server nur dieses Format unterstützt. Deshalb verwende ich den destinationType = 0 (bedeutet DATA_URL). Ich habe jetzt Base64-Bilddaten, aber wie kann ich diese Daten auf SD-Karte speichern?

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

Antwort

0

Da Sie die Daten im Base64-Format haben, können Sie einfach die FileWriter verwenden, um die Daten auf der Festplatte zu speichern.

+0

Hallo Simon Danke für Ihre Antwort retrive, habe ich versucht, "Filewriter", aber nicht in perticular Lage von SD-Karte (wie sdcard/myphotos) speichern successed. Wie diesen Pfad zuweisen? Wissen Sie auch nicht, wie Sie Base64-Daten als gültiges Bild in Phonegap speichern können? Bitte helfen Sie .. – Ram

+0

Hier ist ein gutes Beispiel für die Verwendung des FileWriter. http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#FileWriter_full_example –

2

Hier ist die richtige Antwort auf die ursprüngliche Frage: Wie Sie aufgenommenes Bild in PhoneGap in einen Ordner in SD-Karte verschieben?

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 

kredit an: nbk auf der google phonegap gruppe.

+0

Es ist ein wenig verschachtelt ist es nicht? Warum kann es nicht als Methode mit zwei Parametern implementiert werden, vonURI und toURI. Ich suche nicht nach Ihrer Lösung, ich frage mich nur, warum es nicht besser wäre, es so zu machen und wenn es möglich ist? –

+1

Ich erhalte einen Fehler bei window.resolveLocalFileSystemURI(). –

0

Ich denke, Sie müssen das Bild als FILE_URL erfassen, und speichern Sie das Bild zuerst auf sdcard wie von Steven Benjamin oben erwähnt.

Dann können Sie die Base64 DATA_URL als

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);}); 
} 
Verwandte Themen