2013-08-29 3 views
1

나는 몇 가지 문제에 대해 여기에서 물어보고 싶습니다.이미지 파일로 안드로이드에 sd 카드에 파일을 작성

PhoneGap을 사용하여 사진을 찍은 다음 사진을 캔버스에 표시 할 수있는 응용 프로그램을 만듭니다. 캔버스의 drawimage 이후에 캔버스를 이미지 파일로 변환하는 방법을 사용합니다. 하지만 Android의 SD 카드에 이미지 파일로 파일을 쓰는 것과 관련된 문제가 있습니다. 즉, SD 카드에서 생성 된 이미지 파일을 읽을 수 없습니다 (이미지가 유효하지 않습니다). 여러분의 도움과 제안에 대한

var picture = ""; 
function takePhoto() { 
    navigator.camera.getPicture(onCameraSuccess, 
    onCameraError,{ 
     quality : 50, 
     destinationType : Camera.DestinationType.FILE_URI 
     //saveToPhotoAlbum: true 
     }); 
} 

function onCameraSuccess(imageURL) { 
    var canvas = document.getElementById('myCanvas');  
    var ctx=canvas.getContext("2d"); 
    var imageObj = new Image(); 
    imageObj.onload = function() { 
    ctx.drawImage(imageObj, 0, 0,220,180); 
    }; 
    imageObj.src=imageURL; 
    picture = imageURL; 

} 
function onCameraError(e) { 
    console.log(e); 
    navigator.notification.alert("onCameraError: " + e +" (" + e.code + ")"); 
} 

function storePhoto() {     
    movePic(pictures); 
} 

function movePic(){ 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) {   
    fileSystem.root.getFile("test.PNG", {create: true, exclusive: false}, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    fileEntry.createWriter(gotFileWriter, fail); 
} 

function gotFileWriter(writer) {  
    var c = document.getElementById('myCanvas');  
    var img_from_canvas=c.toDataURL("image/png"); // base64 encoded 
    var pic = img_from_canvas.split("base64,"); 
    var pictures =window.atob(pic[1]);   // decode base64 
    writer.write(pictures); 
    alert("Your picture was successfully stored !") 
} 

function fail(error) { 
    console.log(error.code); 
} 

나는 감사하고 :

여기 내 코드입니다.

+0

캔버스로 캡처 한 후에 이미지 편집 작업을하고 있습니까? – amorbytes

답변

0

캔버스로 캡처 한 후에 이미지 편집 작업을하고 계십니까? 하지 않을 경우 당신은 루트 폴더에 파일을 이동하려면이 코드를 시도 할 수 있습니다

var fs; 

function movePic(){ 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) {   
    fs = fileSystem; 
    window.resolveLocalFileSystemURI(picture, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    fileEntry.moveTo(fs.root,fileEntry.name, success, fail); 
} 

function fail(error) { 
    console.log(error.code); 
} 
+0

나는하려고 애썼지 만 오류 코드가 있습니다 : 5이 코드는 window.resolveLocalFileSystemURI (그림, gotFileEntry, 실패); "그림"은 내 이미지의 경로이지만 내 이미지는 "base64"인코딩이라고 생각합니다. – Sokly

+0

캔버스에서 그림을 편집 한 다음 캔버스를 다음 그림을 사용하여 이미지에 저장해야합니다. canvas.toDataURL ("image/png"); 그래서 저는 캔버스에서 base64 코드를 얻었습니다. – Sokly

+0

캔버스 편집 된 사진을 저장하는 데 도움을 드릴 수 없습니다. ''picture'' 함수는''onCameraSuccess'' 함수에서 params로 얻은''imageURL ''과 같아야합니다. – amorbytes

0

당신은 writer.write (사진)에 문제가;

이 대신

var buffer = new ArrayBuffer(pictures.length); 
var array = new Uint8Array(buffer); 
    for (var i = 0; i < pictures.length; i++) { 
      array[i] = pictures.charCodeAt(i); 
     } 
writer.write(buffer); 

그것은 나를 위해 작동하십시오.

관련 문제