0

I am trying to send a bitmap (image captured in camera - Android) to nodejs. I am using AsycnHttpClient for the same. I am able to successfully send query parameters. I did refer to some of the links where the files are being sent and received. I tried the same, however, I guess nodejs code needs to be different for a bitmap and file (not sure?!)

Please can you help?

Client side code:

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");
            }
        });
    }

Server side code to receive the parameter:

//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"});
});
  • I wonder why you mess around with a Bitmap if all that you want is uploading a file. Upload that file! – greenapps Oct 17 '16 at 15:29
  • As mentioned, it a photo captured from the camera. Hence the bitmap. Could that be an issue? – Hariprasauth Ramamoorthy Oct 17 '16 at 15:36
  • Sorry but i deny the 'hence'. If you capture an image with a camera app the app will create a file. There is no bitmap involved. Except for a thumbnail bitmap depending in the intent used. So please be clear. What are you doing? – greenapps Oct 17 '16 at 15:44

2 Answers2

0

Please make sure your NodeJS server supports multipart first.

Darcy
  • 106
  • 1
  • 5
0

Yes.. I was able to arrive at the solution finally. I encoded the bitmap on base64 to stringify the same. That was stored post the compression. I retrieved it based and decoded the same to bitmap. It worked.