我正在尝试访问 android 系统的照片库以检索图像。我有 navigator.camera.getPicture 函数提供的 imageURI 变量。在那之前没关系。但后来,我想访问照片库并获取此图像的 base64 代码。
由于 navigator.camera.getPicture 不可能同时返回数据(imageURI 和 imageData),我需要稍后获取 base64 信息。这是我尝试使用的代码,查看phoneGap的“文件”文档,但它不起作用。
它在“fileSystem.root.getFile”调用处停止-(错误回调中的错误:File4 = TypeError:表达式'evt.target'[未定义]的结果不是对象。在文件:///android_asset/www/phonegap- 1.3.0.js:717)
谁能帮帮我?谢谢。
function base64(imageURI) {
alert(imageURI);
document.addEventListener("deviceready", onDeviceReady);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);}
function gotFS(fileSystem) {
alert("filesystem");
//Next line causes error. Perhaps imageURI is not a valid path?
fileSystem.root.getFile(**imageURI**, null, gotFileEntry, fail);}
function gotFileEntry(fileEntry) {
alert("gotfileentry");
fileEntry.file(gotFile, fail);}
function gotFile(file){
alert("got file");
readDataUrl(file);}
function readDataUrl(file) {
alert("readDataURL");
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as data URL");
alert(evt.target.result);
};
reader.readAsDataURL(file);
}
function fail(evt) {
console.log(evt.target.error.code);}}