问题
I develop application using Phonegap (Cordova) and I have a problem trying to convert canvas to imageURI and to save it into sdcard.
Here is my code:
function saveCanvasAsImage(imageURI) {
var gotFileEntry = function(fileEntry) {
var gotFileSystem = function(fileSystem) {
var d = new Date();
var n = d.getTime();
// copy the file
fileEntry.moveTo(fileSystem.root.feelathome, n + ".jpg", null, null);
};
// get file system to copy or move image file to
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, fsFail);
alert("Image Capture Success");
};
//resolve file system for image
window.resolveLocalFileSystemURI(document.getElementById("canvas").toDataURL("image/png").replace(/^data:image\/(png|jpg);base64,/, ""), gotFileEntry, fsFail);
}
//file system fail
function fsFail(error) {
alert("failed with error code: " + error.code);
}
and i'm got error failed 5 where i'm wrong?
回答1:
Canvas doesn't work correctly in most versions of android's webview.
To be able to convert correctly a canvas to a file in any version of android, you can use the Canvas2ImagePlugin : https://github.com/devgeeks/Canvas2ImagePlugin
来源:https://stackoverflow.com/questions/23043014/convert-canvas-to-imageuri