I am trying to upload an image but I am getting some out of memory errors if the image file is over ~2 megs on an Android device.
File file = new File(<my image file>);
FileInputStream fis = new FileInputStream(file);
HttpClient client = new HttpClient();
PutMethod method = new PutMethod(myUrl);
RequestEntity requestEntity = new InputStreamRequestEntity(fis);
method.setRequestEntity(requestEntity);
client.executeMethod(method);
The out of memory error happens on the executeMethod line. How can I change this to read the stream as it goes along instead of reading the entire stream into memory and causing a crash?
Sample of hte DDMS output:
07-27 10:46:24.334: DEBUG/dalvikvm(26576): GC freed 6787 objects / 522592 bytes in 119ms
07-27 10:46:24.374: DEBUG/dalvikvm(26576): GC freed 1738 objects / 196752 bytes in 33ms
07-27 10:46:24.374: INFO/dalvikvm-heap(26576): Grow heap (frag case) to 5.184MB for 253968-byte allocation
07-27 10:46:24.414: DEBUG/dalvikvm(26576): GC freed 0 objects / 0 bytes in 45ms
07-27 10:46:24.464: DEBUG/dalvikvm(26576): GC freed 3 objects / 122992 bytes in 48ms
07-27 10:46:24.474: INFO/dalvikvm-heap(26576): Grow heap (frag case) to 5.558MB for 516112-byte allocation
07-27 10:46:24.524: DEBUG/dalvikvm(26576): GC freed 0 objects / 0 bytes in 57ms
07-27 10:46:24.574: DEBUG/dalvikvm(26576): GC freed 3 objects / 254056 bytes in 46ms
07-27 10:46:24.754: INFO/dalvikvm-heap(26576): Grow heap (frag case) to 6.308MB for 1040400-byte allocation
07-27 10:46:24.824: DEBUG/dalvikvm(26576): GC freed 3 objects / 516168 bytes in 63ms
07-27 10:46:24.894: INFO/dalvikvm-heap(26576): Grow heap (frag case) to 7.808MB for 2088976-byte allocation
07-27 10:46:24.994: DEBUG/dalvikvm(26576): GC freed 0 objects / 0 bytes in 101ms
07-27 10:46:25.034: DEBUG/dalvikvm(26576): GC freed 3 objects / 1040456 bytes in 34ms
07-27 10:46:25.084: INFO/dalvikvm-heap(26576): Grow heap (frag case) to 10.808MB for 4186128-byte allocation
07-27 10:46:25.154: DEBUG/dalvikvm(26576): GC freed 0 objects / 0 bytes in 63ms
07-27 10:46:25.434: DEBUG/dalvikvm(26576): GC freed 3 objects / 2089032 bytes in 58ms
07-27 10:46:25.714: INFO/dalvikvm-heap(26576): Grow heap (frag case) to 16.808MB for 8380432-byte allocation
07-27 10:46:25.784: DEBUG/dalvikvm(26576): GC freed 0 objects / 0 bytes in 70ms
07-27 10:46:26.204: DEBUG/dalvikvm(26576): GC freed 3 objects / 4186184 bytes in 65ms
07-27 10:46:26.204: INFO/dalvikvm-heap(26576): Forcing collection of SoftReferences for 16769040-byte allocation