从文本文件读取记录时更新进度条的正确方法-Android
我正在尝试读取应用程序中的大型文本文件,为此我需要使用AsyncTask。我修改了我的代码(它工作得很好)来读取小文件以在AsyncTask中工作。我的问题是更新ProgressBar 我试图让它与问题的答案一起工作:,内容如下: “使用文件大小并计算每次读取的字节数怎么样?” 我尝试在我的应用程序中使用它,但我无法使它工作。 这是我的代码:从文本文件读取记录时更新进度条的正确方法-Android,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,我正在尝试读取应用程序中的大型文本文件,为此我需要使用AsyncTask。我修改了我的代码(它工作得很好)来读取小文件以在AsyncTask中工作。我的问题是更新ProgressBar 我试图让它与问题的答案一起工作:,内容如下: “使用文件大小并计算每次读取的字节数怎么样?” 我尝试在我的应用程序中使用它,但我无法使它工作。 这是我的代码: private class MiTarea extends AsyncTask<String, Float, String > {
private class MiTarea extends AsyncTask<String, Float, String > {
protected void onPreExecute() {
dialog.setProgress(0);
dialog.setMax(100);
dialog.show(); //Mostramos el diálogo antes de comenzar
}
protected String doInBackground(String... urls) {
if (carpeta_para_leer == "Textos"){
sdcard = new File( Environment.getExternalStorageDirectory().toString()+"/" + carpeta_para_leer + "/");
}else{
sdcard = new File( Environment.getExternalStorageDirectory().toString()+"/Textos/" + carpeta_para_leer + "/");
}
//Get the text file
File file = new File(sdcard, nombre_texto + ".txt");
sizeInBytes = file.length();
currentBytes = 0;
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Cp1252"));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
currentBytes += (long)line.length(); //HERE I TRY TO UPDATE THE PROGRESSBAR
}
}
catch (IOException e) {
}
String nuevoTexto = text.toString().replaceAll("\t", " ");
String nuevoTextoA = nuevoTexto.replaceAll("\n", " ");
Holmes1 = nuevoTextoA;
delimitadores = " ";
tokenHolmes1 = new StringTokenizer(Holmes1, " ");
arrayHolmes1 = Holmes1.split(delimitadores);
return Holmes1;
}
protected void onProgressUpdate (Float... valores) {
dialog.setProgress((int)(currentBytes/sizeInBytes)*100);
}
protected void onPostExecute(Integer bytes) {
dialog.dismiss();
}
}
私有类MiTarea扩展异步任务{
受保护的void onPreExecute(){
对话框。设置进度(0);
dialog.setMax(100);
dialog.show();//Mostramos el diálogo antes de comenzar
}
受保护的字符串doInBackground(字符串…URL){
如果(地毯长度=“文本”){
sdcard=新文件(Environment.getExternalStorageDirectory().toString()+“/”+ruepara\u para\u leer+“/”);
}否则{
sdcard=新文件(Environment.getExternalStorageDirectory().toString()+“/Textos/”+ru毯a_para_leer+“/”;
}
//获取文本文件
文件文件=新文件(SD卡,名称为“.txt”);
sizeInBytes=file.length();
currentBytes=0;
//从文件中读取文本
StringBuilder text=新的StringBuilder();
试一试{
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件),“Cp1252”);
弦线;
而((line=br.readLine())!=null){
text.append(行);
text.append('\n');
currentBytes+=(长)line.length();//这里我尝试更新PROGRESSBAR
}
}
捕获(IOE异常){
}
字符串nuevoTexto=text.toString().replaceAll(“\t”,”);
字符串nuevoTextoA=nuevoTexto.replaceAll(“\n”,”);
Holmes1=新文本;
"爱",;
tokenHolmes1=新的StringTokenizer(Holmes1,“”);
arrayHolmes1=Holmes1.分裂(分界);
返回霍姆斯1;
}
受保护的void onProgressUpdate(浮动…valores){
对话框.setProgress((int)(当前字节/大小字节)*100);
}
受保护的void onPostExecute(整数字节){
dialog.dismise();
}
}
您应该调用publishProgress(//此处的值)
进入doInbackground()
这将在内部调用onProgressUpdate
我已经发布了这方面的速记示例…看看这些方法是如何工作的
希望这能帮助…实现运行带有回复的ProgressBar,但是我的代码无法达到100%。有什么建议吗@尼特斯
private class MiTarea extends AsyncTask<String, Float, String > {
protected void onPreExecute() {
dialog.setProgress(0);
dialog.setMax(100);
dialog.show(); //Mostramos el diálogo antes de comenzar
}
protected String doInBackground(String... urls) {
if (carpeta_para_leer == "Textos"){
sdcard = new File( Environment.getExternalStorageDirectory().toString()+"/" + carpeta_para_leer + "/");
}else{
sdcard = new File( Environment.getExternalStorageDirectory().toString()+"/Textos/" + carpeta_para_leer + "/");
}
//Get the text file
File file = new File(sdcard, nombre_texto + ".txt");
sizeInBytes = file.length();
currentBytes = 0;
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Cp1252"));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
currentBytes += (long)line.length(); //HERE I TRY TO UPDATE THE PROGRESSBAR
}
}
catch (IOException e) {
}
String nuevoTexto = text.toString().replaceAll("\t", " ");
String nuevoTextoA = nuevoTexto.replaceAll("\n", " ");
Holmes1 = nuevoTextoA;
delimitadores = " ";
tokenHolmes1 = new StringTokenizer(Holmes1, " ");
arrayHolmes1 = Holmes1.split(delimitadores);
return Holmes1;
}
protected void onProgressUpdate (Float... valores) {
dialog.setProgress((int)(currentBytes/sizeInBytes)*100);
}
protected void onPostExecute(Integer bytes) {
dialog.dismiss();
}
}