android TextView显示容量很大的.txt文件

son123qaz123 2012-01-11 10:46:45
写了个阅读txt文件的小程序显示数据时候数据太多导致UI线程阻塞,界面卡住,有没有方法分段的读取数据并且在TextView显示啊求高手解决啊!
代码
public class TextActivity extends Activity {

private TextView text_title;
private DigitalClock clock;
private TextView myView;// 文本
private ImageView menu_button;// 菜单按钮
private ImageView close_button;// 取消按钮
private String textPath;// 获取文本文件的路径
private ProgressDialog progressDialog;
private StringBuffer stringBuffer;// 存放加载的数据
private static final int finsh_id = 1;
private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Mini文件阅读器");
setContentView(R.layout.text_rw);
text_title = (TextView) findViewById(R.id.text_title);
clock = (DigitalClock) findViewById(R.id.clock);
myView = (TextView) findViewById(R.id.myView);

menu_button = (ImageView) findViewById(R.id.menu_button);
menu_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(TextActivity.this,
MenuActivity.class);
startActivity(intent);
}
});
close_button = (ImageView) findViewById(R.id.close_button);
close_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
TextActivity.this.finish();
}
});
textPath = getIntent().getExtras().getString("textPath");

String text_name = textPath.substring(textPath.lastIndexOf("/") + 1,
textPath.lastIndexOf("."));// 获得文件名称,不包含扩展名称
text_title.setText(text_name);

HandlerThread handlerThread = new HandlerThread("handlerThread");// 创建异步对象
handlerThread.start();
Handler threadHandler = new Handler(handlerThread.getLooper()) {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
convertCodeAndGetText(textPath);// 加载数据并且转码
handler.sendEmptyMessage(finsh_id);
}

};
Message message = threadHandler.obtainMessage();
message.sendToTarget();// 启动异步对象

handler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == finsh_id) {
progressDialog.dismiss();
myView.setText(stringBuffer.toString());//显示数据
}

}

};

progressDialog = ProgressDialog.show(this, "请稍后...", "加载数据中...", false);
}

public void convertCodeAndGetText(String path) {// 转码

stringBuffer = new StringBuffer();

File file = new File(path);

BufferedReader reader;
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);// 找到文档的前三个字节并自动判断文档类型。
in.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));

} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(
new InputStreamReader(in, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,
"utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,
"utf-16le"));
} else {
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
}

String str;

while ((str = reader.readLine()) != null) {
stringBuffer.append(str + "\n");
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
}

}
...全文
736 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
木糖醇的爸爸 2012-01-28
  • 打赏
  • 举报
回复
阅读器的textView是不用全部把文本读进去的 一般情况下是预读当前页的后几页 然后翻一页读一页。。一下子读完完全就是很容易卡住死机的而且耗内存 具体算法自己实现。。。
bcf102 2012-01-21
  • 打赏
  • 举报
回复
RandomAccessFile
用这个读就行了,具体用法自己查

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧