编码图像为Base64并上传到PhoneGap的Web服务 [英] Encoding a image to base64 and upload to web service in Phonegap

查看:109
本文介绍了编码图像为Base64并上传到PhoneGap的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PhoneGap项目中,我使用的 navigator.device.capture.captureImage(captureSuccess,captureError,{限制:1}); 的拍摄照片。在captureSucces功能,我得到德媒体文件,而我EN code它为Base64这一点:

In my PhoneGap project, I use navigator.device.capture.captureImage(captureSuccess, captureError, {limit : 1}); to take a picture. In captureSucces function, I get de MediaFile, and I encode it to base64 with this:

var file="";
var datafile=mediaFiles[0];

var reader = new FileReader();
reader.onload = function(evt){
  file=evt.target.result;
};
reader.readAsDataURL(datafile);

我将此文件发送到一个REST Web服务,在那里我去code中的文件,并保存在文件夹中:

I send this file to a REST Web Service, where I decode the file and save in a folder:

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)。这是因为该文件的价值是不正确的,它包含了:

Unfortunately, I get an empty image(1 KB). This is because the file value is not correct, it contains:

{"name":"1385711756945.jpg",
"fullPath":"file:///storage/sdcard0/DCIM/Camera/1385711756945.jpg",
"type":"image/jpeg",
"lastModifiedDate":1385711757000,
"size":2785413,
"start":0,
"end":0}

当我连接code,我得到一点点的base64 code:

and when I encode this, I get a little base64 code:

[B@877d81

在其他的例子,他们一直使用的输入类型的文件的获取文件(<一href="http://thiscouldbebetter.word$p$pss.com/2013/07/03/converting-a-file-to-a-base64-dataurl-in-javascript/" rel="nofollow">http://thiscouldbebetter.word$p$pss.com/2013/07/03/converting-a-file-to-a-base64-dataurl-in-javascript/)但我得从相机文件。

In other examples, they always use input type file to get the file (http://thiscouldbebetter.wordpress.com/2013/07/03/converting-a-file-to-a-base64-dataurl-in-javascript/) but I have to get the file from Camera.

这是什么问题?一些其他的解决办法?

What is the problem? Some other solution?

非常感谢你。

推荐答案

要做到这一点是使用navigation.camera的最佳方式:

The best way to do this is using navigation.camera:

https://gist.github.com/pamelafox/2173589

这篇关于编码图像为Base64并上传到PhoneGap的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆