Android Reading Text File With SDcard Over 5 MB Errors

I am reading a text file from an SD card. My code works if the file size is small, but when the files are larger than 4 or 5 mb, it gives me an error.

Here is the code I used:

File myFile = new File(""+listAllSdCardFile.get(filePostion)); BufferedReader br1 = new BufferedReader( new FileReader( myFile ) ); StringBuffer text = new StringBuffer(); for(String line; (line=br1.readLine())!=null;) { text.append( line ); } line=text.toString(); 

This is the error I get:

 02-05 07:12:55.184: E/AndroidRuntime(778): FATAL EXCEPTION: Background 02-05 07:12:55.184: E/AndroidRuntime(778): java.lang.OutOfMemoryError 02-05 07:12:55.184: E/AndroidRuntime(778): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97) 02-05 07:12:55.184: E/AndroidRuntime(778): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:157) 02-05 07:12:55.184: E/AndroidRuntime(778): at java.lang.StringBuffer.append(StringBuffer.java:215) 02-05 07:12:55.184: E/AndroidRuntime(778): at com.reader.fastreader.TextReader.StringSpilitFunction(TextReader.java:344) 02-05 07:12:55.184: E/AndroidRuntime(778): at com.reader.fastreader.TextReader$3$1$1.run(TextReader.java:134) 02-05 07:12:55.184: E/AndroidRuntime(778): at java.lang.Thread.run(Thread.java:1096) 02-05 07:12:55.784: E/WindowManager(778): Activity com.reader.fastreader.TextReader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f74708 that was originally added here 02-05 07:12:55.784: E/WindowManager(778): android.view.WindowLeaked: Activity com.reader.fastreader.TextReader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f74708 that was originally added here 02-05 07:12:55.784: E/WindowManager(778): at android.view.ViewRoot.<init>(ViewRoot.java:247) 02-05 07:12:55.784: E/WindowManager(778): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 02-05 07:12:55.784: E/WindowManager(778): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 02-05 07:12:55.784: E/WindowManager(778): at android.view.Window$LocalWindowManager.addView(Window.java:424) 02-05 07:12:55.784: E/WindowManager(778): at android.app.Dialog.show(Dialog.java:241) 02-05 07:12:55.784: E/WindowManager(778): at com.reader.fastreader.TextReader$3$1.onItemClick(TextReader.java:140) 02-05 07:12:55.784: E/WindowManager(778): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 02-05 07:12:55.784: E/WindowManager(778): at android.widget.ListView.performItemClick(ListView.java:3382) 02-05 07:12:55.784: E/WindowManager(778): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 02-05 07:12:55.784: E/WindowManager(778): at android.os.Handler.handleCallback(Handler.java:587) 02-05 07:12:55.784: E/WindowManager(778): at android.os.Handler.dispatchMessage(Handler.java:92) 02-05 07:12:55.784: E/WindowManager(778): at android.os.Looper.loop(Looper.java:123) 02-05 07:12:55.784: E/WindowManager(778): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-05 07:12:55.784: E/WindowManager(778): at java.lang.reflect.Method.invokeNative(Native Method) 02-05 07:12:55.784: E/WindowManager(778): at java.lang.reflect.Method.invoke(Method.java:521) 02-05 07:12:55.784: E/WindowManager(778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 07:12:55.784: E/WindowManager(778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-05 07:12:55.784: E/WindowManager(778): at dalvik.system.NativeStart.main(Native Method) 

What is wrong with my code?

+4
source share
2 answers

This is not about reading a file. You have lost memory because your string is very large and you cannot store it in memory. Try freeing up memory (delete some unused data, 5 not too much) or change the structure of the program.

+1
source

You simply are not in memory, the file is read correctly. As already mentioned, the first option is to see where you can free some heap heap in your application. However, if you have a slightly larger file, it may break again.

You need to answer this question: why do I need all the text at once in my memory? And if you do not find the final point for this, you will need to answer the following question: how will I do my algorithm / processing / display of the file in pieces, so I only need to save part of the file in memory.

With more information on what you need to do with this text, you will get more information on how to do it differently if you need to.


Edit:

instead of displaying the entire contents of the file immediately in the editing text, you can, for example, show pages: read part of the file (say, for example, 100 lines). Once the user reaches the bottom of the current page, load the next 100 lines from the file (and, of course, discard the previous 100).

You can do this with ViewPager (each fragment shows 100 lines, swipe left to display the next page of text, ...)

For the first time, you can view the entire file to calculate the total number of lines (without saving the text) and, thus, indicate the number of pages that will do.

+1
source

All Articles