phonegap android无法读取图像文件

时间:2014-02-19 07:14:17

标签: android phonegap-plugins cordova-plugins

我正在使用phonegap 3.3开发混合。我正在使用相机插件捕捉图像并存储到相册中,工作正常。稍后,我必须从设备存储中读取图像文件。

我正在使用以下代码。

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, failFS);
function gotFS(fileSystem){
    fileSystem.root.getFile(imageData, {create: true}, gotFileEntry, fail);
}
function gotFileEntry(){
    fileEntry.file(gotFile,fail);
}
function gotFile(file){
    alert(file.getParent().fullPath);
}

我在第一行收到错误。它正在给予

  

FileError.ENCODING_ERR

我不确定我在这里做错了什么。之后,我必须使用新名称移动到另一个目录。任何人都可以帮我解决。

我正在使用相机插件来捕获图像和文件插件来读取文件和目录。

- Sridhar

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码来捕获和复制图像

var pictureSource;
var destinationType;

var FileFolder = "";
var FileName = "";

var obj_imageCapture = {
    capturePicture:function(imgFolder,imgName)
    {

        var image = imgFolder + imgName;

        FileFolder = imgFolder;
        FileName = imgName;

        pictureSource = navigator.camera.PictureSourceType;
        destinationType = navigator.camera.DestinationType;

        navigator.camera.getPicture(obj_imageCapture.onPhotoDataSuccess1, obj_imageCapture.onFail, {quality: 50, destinationType: destinationType.FILE_URI , saveToPhotoAlbum: true });

     },
     onPhotoDataSuccess1:function(imageData){

     obj_imageCapture.createFileEntry(imageData);


   },
   createFileEntry:function(imageURI) {
      window.resolveLocalFileSystemURI(imageURI, obj_imageCapture.copyPhoto, obj_imageCapture.onFFail);
   },
   copyPhoto:function(fileEntry) {
   try
   {


        var ext = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('.'));



        var imageN = "";

        if(FileName.indexOf('.') > 0)
        {
            imageN = FileName.substr(0,FileName.lastIndexOf('.')) + ext;
        }
        else
        {
            imageN = FileName + ext;
        }




        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
                                 fileSys.root.getDirectory(FileFolder, {create: true, exclusive: false}, function(dir) {
                                                           fileEntry.copyTo(dir, imageN, obj_imageCapture.onCopySuccess, obj_imageCapture.onFFail);
                                                           }, obj_imageCapture.onFFail);
                                 }, obj_imageCapture.onFFail);
    }
    catch(ex)
    {
        alert(ex.message);
    }

    },
    onCopySuccess:function(entry) {


        var smallimage = document.getElementById("myimage");
        smallimage.style.display = "block";
        smallimage.src = entry.fullPath + "?rand=" + Math.random();



    },
    onFFail:function(message)
    {
        alert("Error in photo : " + message.message);
    }
};

以上代码可能对您有所帮助