I write a file browser application, but then read some files folder have more time will be required to yo very long time, I would like to have time to click on the folder to add a progressdialog, but does not know how to determine the file has been read end and close this dialog, seeking expert guidance.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Read the stream, if it is to return NULL character stream generally said to read the end.
If the bytes read, -1 is generally the end.
public static String readFileContent (String absPath) throws IOException {
File file = new File (absPath);
if (file.exists ()) {
BufferedReader bw = new BufferedReader (new FileReader (file));
StringBuffer sb = new StringBuffer ();
String line = null;
while ((line = bw.readLine ())! = null) {
sb.append (line);
}
bw.close ();
return sb.toString ();
}
return null;
} public static byte [] readInputStreamByte (InputStream ins) {
if (ins == null) {
return null;
}
BufferedInputStream bis = new BufferedInputStream (ins);
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
try {
byte [] buffer = new byte [128];
int n = -1;
while ((n = bis.read (buffer))! = -1) {
bos.write (buffer, 0, n);
}
} Catch (IOException e) {
e.printStackTrace ();
return null;
} Finally {
if (bis! = null) {
try {
bis.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
return bos.toByteArray ();
} Reply:
If you are one of the files, go to the list of file size, and then determine whether it is the last file. This good deal.
Reply:
Read a file, and now I practice
protected void onListItemClick (ListView l, View v, int position, long id) {
File file = new File (paths.get (position));
currentlist.clear ();
currentlist.addAll (paths);
/ / Currentlist = paths;
if (file.isDirectory ()) {
showDialog (DATAREADING);
item_position_selected = getListView () getSelectedItemPosition ();.
item_position_first = getListView () getFirstVisiblePosition ();.
View cv = getListView () getChildAt (item_position_selected - item_position_first);.
if (cv! = null) {
fromtop_piexl = cv.getTop ();
}
BrowserFile (paths.get (position));
if (! listFiles.isEmpty ()) {
fileDirectory_position_selected.add (item_position_selected);
fileDirectory_position_piexl.add (fromtop_piexl);
pathLevel + +;
}
} Now like to read the file list is displayed after the pop-up dialog ~
Reply:
Is the need to add a process for processing the file to read it
Reply:
Is certainly needed, even after reading the thread has finished reading, the last face you perform in threaded code, add a message handler can be
Reply:
Thread reads the file, and then read over, informs about the UI thread with close Dialog.
new Thread () {
public void run () {
int n = FileUtils.Process () ;/ / assume that such treatment
if (n == 0) {/ / properly processed
handler.sendEmptyMessage (0); / / has finished processing, send the message.
}
}
}. Start ()
Reply:
5th Floor, positive solutions

Reply:
Why phenomenon is to show the file list and then display progressDialog it, if the thread is in onClickListener inside plus it?
Reply:
I call progressDialog time to deal with how to operate it in the background file read
Reply:
/ / Dialog is only displayed to the user to improve the experience, and file manipulation okay
Dialog progressDialog = new ProgressDialog (this);
progressDialog.show ();
new Thread () {
public void run () {
int n = FileUtils.Process () ;/ / assume that such treatment
if (n == 0) {/ / properly processed
handler.sendEmptyMessage (0); / / has finished processing, send the message. Dialog is closed to stop processed.
}
}
}. Start ()
Reply:
Get a File object folder when its not there listFiles method returns a Files [] array of objects with a length property it is not know how many files in this folder it? Know the total number, do not know when reading is completed?
Reply:
Thank you for your help, with the thread load, blocked prgressDiaolog realized. Results posted
No comments:
Post a Comment