【问题标题】:Write file to sd card in android as image file将文件作为图像文件写入android中的sd卡
【发布时间】:2013-08-29 08:14:23
【问题描述】:

我想在这里问你一些问题。

我正在使用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);
}

感谢您的帮助和建议。

【问题讨论】:

  • 您是否在通过画布捕获后对图像进行任何类型的编辑?

标签: javascript android canvas cordova


【解决方案1】:

您是否在通过画布捕获后对图像进行任何类型的编辑? 如果没有,那么您可以尝试使用此代码将文件移动到根文件夹

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

【讨论】:

  • 我已经尝试过,但是我得到了错误代码:5 这个代码 window.resolveLocalFileSystemURI(picture, gotFileEntry, fail);我认为“图片”是我的图像的路径,但我的图像是“base64”编码。
  • 我必须在画布中编辑图片,然后使用:canvas.toDataURL("image/png"); 将该画布保存到图像中。所以我从画布上得到了 base64 代码。
  • 我无法帮助您保存画布编辑的图片。并且"picture" 应该与"imageURL" 相同,您在函数"onCameraSuccess" 中作为参数获得。
  • 感谢您的回复。顺便说一句,我会尝试寻找其他解决方案。
【解决方案2】:

您对 writer.write(pictures) 有疑问;

试试这个

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

它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多