如何以特殊方式读取从 uri 获取的文本文件(大小约为 2Mb),而不需要将屏幕黑一分钟然后显示整个字符串?我的问题是我将 textView 文本设置为整个字符串,这就是它滞后的原因。有没有办法让它分小部分读取,然后分小部分显示?以 ES 文件浏览器的方式。
这是我实际的、效率不高且滞后的代码:
InputStream inputStream = null;
String str = "";
StringBuffer buf = new StringBuffer();
TextView txt = (TextView)findViewById(R.id.textView);
try {
inputStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
if (inputStream!=null) {
try {
while ((str = reader.readLine()) != null) {
buf.append(str + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
txt.setText(buf.toString());
}
最佳答案
我的第一个想法如下:
txt.setText("");
if (inputStream!=null) {
try {
while ((str = reader.readLine()) != null) {
txt.append(str + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
关于java - Android 根据大小分多个步骤加载字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28252718/