我再写一个文件浏览器应用,但是再读取一些文件较多的文件夹得时候会需哟很长时间,我想再点击文件夹得时候加一个progressdialog,但是不知道如何判断文件读取已经结束,并关闭这个dialog,求高手指导。

解决方案 »

  1.   

    流的读取,如果是字符流一般返回NULL表示读取结束。
    如果是字节读取,一般返回-1是结束。 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();
    }
      

  2.   

    是读的一个个文件,现在我做法是
    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++;
         }
        }
    现像是读取文件列表显示后才弹出对话框~
      

  3.   


    肯定是需要的,线程读取完之后就算读完了,你在线程代码执行的最后面,加一个handler 发消息就可以了
      

  4.   

    线程读取文件,然后读取完了,就用UI线程通知一下,关闭Dialog.
    new Thread(){
       public void run(){
           int n = FileUtils.Process();//假设这样处理
            if(n == 0){ //正常处理完
             handler.sendEmptyMessage(0); //已经处理完了,发送消息。
           }   
      }
    }.start()
      

  5.   

    为什么现象是先显示出文件列表再显示progressDialog呢,线程的话是在onClickListener里面加吗?
      

  6.   

    我的调用progressDialog的时候怎么样在后台去处理文件读取操作呢
      

  7.   

    //dialog只是显示给用户看的,提高体验的,和文件操作没关系
    Dialog progressDialog = new ProgressDialog(this);
    progressDialog.show(); 
    new Thread(){
        public void run(){
            int n = FileUtils.Process();//假设这样处理
            if(n == 0){ //正常处理完
             handler.sendEmptyMessage(0); //已经处理完了,发送消息。处理完Dialog就关闭停止。
           }   
       }
     }.start()
      

  8.   

    获得某文件夹的File对象的时候,其不是有个listFiles的方法,返回一个Files[]的数组对象,同个它的length属性,不是知道这个文件夹下有多少个文件么?知道总数量,难道还不知道什么时候读取完成?
      

  9.   

    谢谢各位帮助,用了线程进行加载,阻塞prgressDiaolog就实现了。结贴了