我想使用 URLEncoder.encode 编码一个 Base64 编码的字符串,这个字符串包含图像,它可以是任意大小(1MB-8MB)。
String imageDataToUpload = URLEncoder.encode(temp, "UTF-8");
02-27 02:41:45.213: D/dalvikvm(18600): GC_FOR_ALLOC freed 1824K, 12% free 26452K/29831K, paused 161ms
02-27 02:41:45.213: I/dalvikvm-heap(18600): Forcing collection of SoftReferences for 2968580-byte allocation
02-27 02:41:45.383: D/dalvikvm(18600): GC_BEFORE_OOM freed 9K, 12% free 26443K/29831K, paused 138ms
02-27 02:41:45.383: E/dalvikvm-heap(18600): Out of memory on a 2968580-byte allocation.
即使我在块中尝试了这个东西,但随后 StringBuffer.append 制造了 OutOfMemoryError
private String encodeChunckByChunck(String str, final int chunkSize) {
StringBuffer buffer = new StringBuffer();
final int size = str.length();
try {
for (int i = 0; i < size; i += chunkSize) {
Log.d("Encode", "..........inside loop");
if (i + chunkSize < size) {
Log.d("Encode", "..........full chunk");
buffer.append(URLEncoder.encode(str.substring(i, i + chunkSize), "UTF-8"));
} else {
Log.d("Encode", "..........half chunk");
buffer.append(URLEncoder.encode(str.substring(i, size), "UTF-8"));
}
}
} catch (UnsupportedEncodingException e) {
Log.d("Encode", "..........exception:" + e.getMessage());
e.printStackTrace();
}
Log.d("Encode", "..........before returning function");
return buffer.toString();
}