Reading a large text file in Textview

I want to read a large file from sdcard into a text view. I have an idea, but I don’t know how to apply.

I think it should be used: Handler and Subject

But I do not know how to apply. Someone will give an example or a tutorial.

Updated:

Thread test=new Thread() { public void run() { File sfile=new File(extras.getString("sfile")); try { StringBuilder text = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(sfile)); String line1; while(null!=(line1=br.readLine())) { text.append(line1); text.append("\n"); } subtitletv.setText(text.toString()); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; test.start(); 

This is my code. But it is better than the previous code, but it cannot read the 2 MB file. How to do it? And how to establish progress?

+6
source share
4 answers

Here is an example of how to do this. If the file is so large that it takes more than 5 seconds to read, then most likely it will be AsyncTask .

 // first open the file and read it into a StringBuilder String cardPath = Environment.getExternalStorageDirectory(); BufferedReader r = new BufferedReader(new FileReader(cardPath + "/filename.txt")); StringBuilder total = new StringBuilder(); String line; while((line = r.readLine()) != null) { total.append(line); } r.close(); // then get the TextView and set its text TextView txtView = (TextView)findViewById(R.id.txt_view_id); txtView.setText(total.toString()); 

EDIT

You can only change user interface elements in the user interface stream. The topic documentation contains more detailed information. When you try to do this from another thread, you get this (from your pastebin):

 E/AndroidRuntime( 8517): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

I think the easiest solution uses AsyncTask , as I recommended earlier. You just put your working code in one function ( doInBackground() ), and your user interface code in another ( onPostExecute() ) and AsyncTask make sure that they are called in the correct threads and in order. The documentation I'm attached to has examples of loading bitmaps, which is roughly the same as loading text.

+9
source

Your problem is that you are accessing the view belonging to the GUI stream from the stream that reads your file:

 subtitletv.setText(text.toString()); 

You need to read the file, then transfer its contents to the main stream that will be displayed.

 //Create a handler on the UI Thread: private Handler mHandler = new Handler(); //Executed in a non-GUI thread: public void updateView(){ final String str = TheDataFromTheFile; mHandler.post(new Runnable(){ @Override public void run() { subtitletv.setText(str); } } } 
+1
source

Create a new stream and then openFileforInput read all the file data in a StringBuffer.

Use the TextView.setText() method to set the data.

0
source

The user interface can be updated using this as well:

 runOnUiThread(new Runnable() { @Override public void run() { } }); 
0
source

Source: https://habr.com/ru/post/904179/


All Articles