【发布时间】:2013-11-29 08:00:22
【问题描述】:
在我的 PhoneGap 项目中,我使用 navigator.device.capture.captureImage(captureSuccess, captureError, {limit : 1}); 来拍照。在 captureSucces 函数中,我得到了 de MediaFile,并将其编码为 base64:
var file="";
var datafile=mediaFiles[0];
var reader = new FileReader();
reader.onload = function(evt){
file=evt.target.result;
};
reader.readAsDataURL(datafile);
我将此文件发送到 REST Web 服务,在那里我将文件解码并保存在一个文件夹中:
if (files != null) {
FileOutputStream fileOutputStream = null;
try {
byte[] byteFichero = Base64.decodeBase64(files);
System.out.println("ARCHIVO " + byteFichero);
File fich = new File(pathFichero.toString());
fich.createNewFile();
fileOutputStream = new FileOutputStream(fich);
fileOutputStream.write(byteFichero);
System.out.println("Fichero almacenado ok");
} catch (Exception e) {
System.out.println("Excepcion alamacenando fichero "
+ e.getMessage() + " " + pathFichero);
return respuesta;
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException ex) {
Logger.getLogger(
GestionFicheroObjetoService.class.getName())
.log(Level.SEVERE, null, ex);
}
pathFichero.delete(0, pathFichero.length());
}
}
不幸的是,我得到一个空图像(1 KB)。这是因为文件值不正确,它包含:
{"name":"1385711756945.jpg",
"fullPath":"file:///storage/sdcard0/DCIM/Camera/1385711756945.jpg",
"type":"image/jpeg",
"lastModifiedDate":1385711757000,
"size":2785413,
"start":0,
"end":0}
当我对此进行编码时,我得到了一些 base64 代码:
[B@877d81
在其他示例中,他们总是使用 输入类型文件 来获取文件 (http://thiscouldbebetter.wordpress.com/2013/07/03/converting-a-file-to-a-base64-dataurl-in-javascript/),但我必须从相机获取文件。
有什么问题?其他解决方案?
非常感谢。
【问题讨论】:
标签: javascript android web-services cordova