I am trying to use the following code to upload a file to a rest api. This code works fine for files up to around 20MB, but bigger files will give an OutOfMemoryError<
Analyzing the code, it seems Spring for Android buffers its data before sending it - that's why your OOE. There is a trick however (but so far I've made it possible to work only for API level 9 above): you can disable the buffering for its connection factory but ONLY if the RestTemplate has no ClientHttpRequestInterceptor
list set (now how stupid is that ?) - lucky you as you didn't set one. So in your case the situation is very simple, just call factory.setBufferRequestBody(false);
after you've instantiated it.