使用AsyncHttpclient

时间:2016-10-17 15:20:50

标签: android node.js bitmap asynchttpclient

我正在尝试将位图(在相机中捕获的图像 - Android)发送到nodejs。我正在使用AsycnHttpClient。我能够成功发送查询参数。我确实参考了一些发送和接收文件的链接。我尝试过相同的,但是,我想nodejs代码需要与位图和文件不同(不确定?!)

请帮忙吗?

客户端代码:

private void storeImage(Bitmap bm){
        AsyncHttpClient client = new AsyncHttpClient();
        //Append the parameters in the Service URL
        RequestParams rp = new RequestParams();
        String SERVICE_URL_REG = SERVICE_URL + "uploadImage?userID=rhari008";
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] data = bos.toByteArray();
        ByteArrayInputStream bInput = new ByteArrayInputStream(data);
        rp.put("image",bInput);
        client.post(SERVICE_URL_REG, rp, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.d(TAG,"Successfully uploaded the image");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.d(TAG,"Error in uploading the image");
            }
        });
    }

接收参数的服务器端代码:

//Image upload
router.post('/uploadImage', function(req, res, next) {
   console.log("Image upload reached for " + req.query.userID);
   var file = req.files.image;
   console.log("File received : ");
   //console.log("Image path : "+req.files.image.path);
   res.json({result:"success"});
});

2 个答案:

答案 0 :(得分:0)

请确保您的NodeJS服务器首先支持多部分。

答案 1 :(得分:0)

是的..我终于能够达成解决方案了。我在base64上对位图进行了编码以对其进行字符串化。那是在压缩后存储的。我检索它并将其解码为位图。有效。