关于android:使用PhoneGap从imageURI获取Base64

Get Base64 from imageURI with PhoneGap

我正在尝试从手机相册中选取的图像中获取base64,但无法使其正常工作:

我试过这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
    console.log("0");
    fileSystem.root.getFile(imageURI, null, function(fileEntry) {
        console.log("1");
        fileEntry.file(function(file) {
            console.log("2");
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read complete!");
                image64.value = Base64.encode(evt.target.result);
            };
            reader.readAsText(file);
        }, failFile);
    }, failFile);
}, failSystem);

尽管图像显示正确。我收到来自此函数的错误:

1
fileSystem.root.getFile(imageURI, null, function(fileEntry)

错误是:fileerror.encodingu err

我知道代码不好看。但我不知道如何从ImageUri中获取base64编码。


从以上的方法,但在serpro厂有两个转变

1
2
3
reader.readAsText(file);
to
reader.readAsDataURL(file);

因此,线

1
image64.value = Base64.encode(evt.target.result);

可以删除和64位的结果可以直接提取AA

1
image64.value = evt.target.result;

我发现在谷歌的解决方案之一。在改性这一点这是结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
var gotFileEntry = function(fileEntry) {
    console.log("got image file entry:" +  fileEntry.fullPath);
    fileEntry.file( function(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read complete!");
            image64.value = Base64.encode(evt.target.result);
        };
        reader.readAsText(file);
    }, failFile);
};

window.resolveLocalFileSystemURI(imageURI, gotFileEntry, failSystem);

注:它需要约20秒钟读一5mpixel正常图像和另一个10到15个Base64编码的信息。


《科尔多瓦-插件文件implements the HTML5文件的API,API,辨别回调的承诺。在prefer SOS的方法使用rewrote问:《美元的图书馆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function getContentAsBase64(fileUrl) {

  //Using $q to change the API so that getContentAsBase64 returns a promise
  var deferred = $q.defer();

  //function to call when either resolve or retrieval fails
  var fail = function (error) {
    errorHandler(error);
    deferred.reject(error);
  }

  //function to call when resolve file succeeded
  //we have a FileEntry - get the file,
  var fileResolved = function (fileEntry) {
    fileEntry.file(fileSuccess, fail);
  }

  //function to call when file successfully retrieved
  //convert to base64 string
  var fileSuccess = function (file) {

    var reader = new FileReader();
    reader.onloadend = function (evt) {
      //promise is resolved with the base64 string
      deferred.resolve(evt.target.result);
    };
    reader.readAsDataURL(file);
  };

  window.resolveLocalFileSystemURL(fileUrl, fileResolved, fail);
  return deferred.promise;
}

The readAsDataURL method is used to read the contents of the
specified Blob or File. When the read operation is finished, the
readyState becomes DONE, and the loadend is triggered.At that time,
the result attribute contains the data as a URL representing the
file's data as a base64 encoded string.

使用:

1
2
3
4
var imageData;
getContentAsBase64(aq.FileUri).then(function (content) {
   imageData= content;
});