PDA

View Full Version : Spring showcase with image from camera.



reddeagle
Mar 1st, 2012, 01:40 PM
Hi There,
I am trying to modify the example "HttpPostFormDataActivity" to use an image taken from the camera rather than an image loaded from the <code>Resource resource = new ClassPathResource("res/drawable/spring09_logo.png");</code>

I have got it all hanging together except one critcal part:

For

formData.add("file", resource);

I had tried :


ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
resource = new ByteArrayResource(bitmapdata);
formData.add("file", resource);


What must resource now be in order for it to be accepted by the Server:




@RequestMapping(value = "postformdata", method=RequestMethod.POST, headers="Content-Type=multipart/form-data")
public @ResponseBody String handleFormUpload(@RequestParam("description") String description, @RequestParam("file") MultipartFile file) {




I was going to go down the route or creating a temp file with the image just taken and use the same code? Is the a correct approach.

Any help / guidance would be appreciated.

Roy Clarkson
Mar 28th, 2012, 01:49 PM
This approach should work. Once you have the file on the server, you can getBytes() from it and persist it to a file or a database, whichever you like.

One addition in Spring Framework 3.1 is the "consumes" parameter. You can update your @RequestMapping as follows:

@RequestMapping(value = "postformdata", method = RequestMethod.POST, consumes = "multipart/form-data")

Here is a nice tutorial on how to retrieve the image from the camera, just as additional information for others.
http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/