1

ここで、ある問題についてお尋ねしたいと思います。

Phonegap を使用して、写真を撮ってキャンバスに画像を表示できるアプリケーションを構築しています。キャンバスに画像を描画した後、キャンバスを画像ファイルに変換するメソッドを使用します。しかし、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);
}

あなたの助けと提案に感謝します。

4

2 に答える 2

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);
}
于 2013-08-29T10:15:29.263 に答える