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

}
...全文
814 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
木糖醇的爸爸 2012-01-28
  • 打赏
  • 举报
回复
阅读器的textView是不用全部把文本读进去的 一般情况下是预读当前页的后几页 然后翻一页读一页。。一下子读完完全就是很容易卡住死机的而且耗内存 具体算法自己实现。。。
bcf102 2012-01-21
  • 打赏
  • 举报
回复
RandomAccessFile
用这个读就行了,具体用法自己查
代码下载链接: https://pan.quark.cn/s/a8fbca3925b4 《鸿蒙OS开发环境构建》指南系统性地阐述了配置和筹备鸿蒙OS开发所需的各种工具和条件的具体方法。鸿蒙OS,亦称HarmonyOS,是由华为研发的一款面向全场景的分布式操作系统,其目标是提供跨平台、多设备间无缝协作的使用体验。本指南涉及了从Linux服务器到Windows工作站的完整开发流程。指南中提及了MobaXterm,这是一款用于连接Linux源码服务器的软件,使得开发人员能够在Windows环境中远程访问Linux服务器。同时,HiTool作为烧录工具,用于将编译后的系统镜像写入开发板。IPOP.EXE则是一款串口终端软件,用于执行串行通信和调试任务。Embedded Studio用于开发设备驱动程序,而DevEco Studio是华为提供的图形化应用程序开发平台,支持C/C++语言,拥有代码编辑、编译、烧录和调试功能,被视为OpenHarmony智能设备开发者的首选集成开发环境。在硬件配置方面,指南列出了必需的设备,包括Linux服务器(推荐Ubuntu 16.04及以上版本),Windows工作台(兼容XP/7/10),以及Hi3518EV300 IoT Camera单板。开发期间,Windows工作台通过USB线与单板相连接,以实现数据传输。此外,为了开展开发工作,还需要安装putty、IPOP、tftp服务器等辅助软件,以及HiTool用于烧录操作。在软件系统要求方面,Linux服务器需要安装bash、Python3.7+、gn、ninja、LLVM等构建工具,这些工具对于生成和执行编译脚本具有关键作用。在Windows工作台上,建议采用Visual Studi...

80,490

社区成员

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

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