文件选择器中的Hinding系统文件夹

时间:2014-07-25 04:36:40

标签: android filepicker

问题: - 此代码适用于拾取文件,但我想在运行此代码时隐藏系统文件夹,它还会显示所有以"开头的android文件夹。"我尝试了一些你可以在我的代码中的评论中看到的东西..

FileChooser.java

package com.sarita.scheduler;

import java.io.File;
import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ListView;

public class FileChooser extends ListActivity {

    private File currentDir;
    private FileArrayAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        currentDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        fill(currentDir); 
    }
    private void fill(File f)
    {
        String rootDriectory =  Environment.getExternalStorageDirectory().getName(); ;

/*      String chkFileName = f.getName().toString();
        String chkSystemFolder =String.valueOf(chkFileName.charAt(0));
        */
        File[]dirs = f.listFiles(); 
        /*if (chkSystemFolder != "."){*/
         this.setTitle("Current Dir: "+f.getName());
         List<Item>dir = new ArrayList<Item>();
         List<Item>fls = new ArrayList<Item>();
         try{



             for(File ff: dirs)
             { 
                Date lastModDate = new Date(ff.lastModified()); 
                DateFormat formater = DateFormat.getDateTimeInstance();
                String date_modify = formater.format(lastModDate);
                if(ff.isDirectory()){


                    File[] fbuf = ff.listFiles(); 
                    int buf = 0;
                    if(fbuf != null){ 
                        buf = fbuf.length;
                    } 
                    else buf = 0; 
                    String num_item = String.valueOf(buf);
                    if(buf == 0) num_item = num_item + " item";
                    else num_item = num_item + " items";

                    //String formated = lastModDate.toString();
                    dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon")); 
                }
                else
                {

                    fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
                }
             }

         }catch(Exception e)
         {    

         }

         Collections.sort(dir);
         Collections.sort(fls);
         dir.addAll(fls);
         if(!f.getName().equalsIgnoreCase(rootDriectory))
             dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
         adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_explorer,dir);
         this.setListAdapter(adapter); 
        /*}*/
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Item o = adapter.getItem(position);
        if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
                currentDir = new File(o.getPath());
                fill(currentDir);
        }
        else
        {
            onFileClick(o);
        }
    }
    private void onFileClick(Item o)
    {
        //Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("GetPath",currentDir.toString());
        intent.putExtra("GetFileName",o.getName());
        setResult(RESULT_OK, intent);
        finish();
    }
}

FileArrayAdapter.java

package com.sarita.scheduler;

import java.util.List; 

import android.content.Context; 
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 
import android.widget.ArrayAdapter;
import android.widget.ImageView; 
import android.widget.TextView;


public class FileArrayAdapter extends ArrayAdapter<Item>{

    private Context c;
    private int id;
    private List<Item>items;

    public FileArrayAdapter(Context context, int textViewResourceId,
            List<Item> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }
    public Item getItem(int i)
     {
         return items.get(i);
     }
     @Override
       public View getView(int position, View convertView, ViewGroup parent) {
       final Item o = items.get(position);
       String chkFileName = o.getName().toString();
       String chkSystemFolder =String.valueOf(chkFileName.charAt(0));


               View v = convertView;
               if (v == null) 
               {
                   LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   v = vi.inflate(id, null);
               }

             /* create a new view of my layout and inflate it in the row */
            //convertView = ( RelativeLayout ) inflater.inflate( resource, null );

               if (chkSystemFolder != ".")
               {        
               if (o != null) 
               {
                       TextView t1 = (TextView) v.findViewById(R.id.TextView01);
                       TextView t2 = (TextView) v.findViewById(R.id.TextView02);
                       TextView t3 = (TextView) v.findViewById(R.id.TextViewDate);
                       /* Take the ImageView from layout and set the city's image */
                        ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
                        String uri = "drawable/" + o.getImage();
                        int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
                        Drawable image = c.getResources().getDrawable(imageResource);
                        imageCity.setImageDrawable(image);
                     if (chkSystemFolder != "."){
                       if(t1!=null)
                        t1.setText(o.getName());
                       if(t2!=null)
                            t2.setText(o.getData());
                       if(t3!=null)
                            t3.setText(o.getDate());
                     }
                   }

       }

       return v;
       }

}

1 个答案:

答案 0 :(得分:1)

for方法的fill()循环中添加以下内容作为第一行:

 if(ff.isHidden()) continue;

File#isHidden()如果隐藏了它所代表的文件或文件夹,则返回true;即如果其文件名以'.'开头。循环内的continue语句会导致跳过循环的当前迭代。所以,这段代码基本上说“如果隐藏了这个文件/文件夹,只需转到下一个。”