I just tested my devices on some older phones and get a java.lang.OutOfMemoryError. Could someone please help me fix this problem?
What im trying to do: Convert a very large string to an html file, the string consists of html text. This is my code:
OutputStream outputStream = null;
InputStream inputStream = null;
try {
inputStream = new ByteArrayInputStream(htmlContent.getBytes(Charset.forName("UTF-16")));
outputStream = new FileOutputStream(new File(dir, appBook.getPath()));
byte[] bufferData = new byte[512];
int bytesRead = inputStream.read(bufferData);
while (bytesRead != -1) {
outputStream.write(bufferData, 0, bytesRead); //add the bufferData data to the "new file"
bytesRead = inputStream.read(bufferData); // keep on reading and filling the dynamic byte araay until it returns -1
}
Its specifically the Charset.forName("UTF-16")) that causes the error on these older devices. If I make the htmlContentString shorter by 2 no error occurs.
This makes me think that the arrays size is to big for the ram? So how should I approach this?
logcat
03-14 16:32:39.067 5604-5604/oskaro.synesthesia.oskar.leonad.synesthesiaconverter I/dalvikvm﹕ [ 03-14 16:32:39.067 5604: 5604 D/AndroidRuntime ]
Shutting down VM
03-14 16:32:39.067 5604-5604/oskaro.synesthesia.oskar.leonad.synesthesiaconverter W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40fde2a0)
03-14 16:32:39.077 5604-5604/oskaro.synesthesia.oskar.leonad.synesthesiaconverter E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at java.nio.charset.CharsetEncoderICU.getArray(CharsetEncoderICU.java:235)
at java.nio.charset.CharsetEncoderICU.encodeLoop(CharsetEncoderICU.java:169)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:415)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:283)
at java.nio.charset.Charset.encode(Charset.java:451)
at java.lang.String.getBytes(String.java:870)
at oskaro.synesthesia.oskar.leonad.synesthesiaconverter.HtmlAddDialog.createHtmlFile(HtmlAddDialog.java:120)
at oskaro.synesthesia.oskar.leonad.synesthesiaconverter.HtmlAddDialog.access$700(HtmlAddDialog.java:39)
at oskaro.synesthesia.oskar.leonad.synesthesiaconverter.HtmlAddDialog$4.onClick(HtmlAddDialog.java:209)
at android.view.View.performClick(View.java:4232)
at android.view.View$PerformClick.run(View.java:17298)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)