将大文本文件读入Textview [英] Read a large text file into Textview

查看:110
本文介绍了将大文本文件读入Textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将SD卡中的大文件读入文本视图.我有主意,但我不知道如何申请.

I want to read large file from sdcard into text view. I have idea but i don't know how to apply.

我认为这件事需要使用:处理程序和线程

I think this things need to use: Handler And Thread

但是我不知道如何申请.任何人都可以举一些例子或教程.

But i dont know how to apply. Anybody give some example or Tutorial.

已更新:

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();

这是我的代码,但是比以前的代码要好,但无法读取2MB的文件.如何解决这个问题?以及如何设置进度?

This is my code.But it is better than previous code, but it is not able to read 2MB file. How to slove this? And How to set Progress?

推荐答案

下面是一个示例.如果文件太大,以至于需要花费大约5秒钟以上的时间来读取,那么它可能应该只是一个

Here's an example of how to do it. If the file is so large that it's going to take more than about 5 seconds to read, then it should probably just be an 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());

编辑

您只能在UI线程中更改UI元素.关于线程的文档中有更多详细信息.当您尝试从另一个线程执行此操作时,(从您的pastebin中)获得了此信息:

You can only change UI elements in the UI thread. The documentation on threads has more details. When you try to do it 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.

我认为最简单的解决方案是使用 AsyncTask ,就像我之前建议的那样.您只需将工作代码放在一个函数( doInBackground())中,并将UI代码放在另一个函数( onPostExecute())中,然后 AsyncTask 确保在正确的线程上按顺序调用它们.我链接到的文档中有加载位图的示例,这与加载文本几乎一样.

I think the easiest solution is using AsyncTask, as I recommended before. You just put your work code in one function (doInBackground()) and your UI code in another (onPostExecute()), and AsyncTask makes sure they get called on the right threads and in order. The documentation I linked to has examples with loading bitmaps, which is just about the same thing as loading text.

这篇关于将大文本文件读入Textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆