0

Question :- This code is working well for picking up the file but i want to hide system folders when I am running this code it also show me all android folder starting with "." I tried some things which you can already see in comments in my code..

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;
       }

}
Kirtesh
  • 29
  • 1
  • 10
  • Perhaps the `File#isHidden()` method is what you're looking for? – Mike M. Jul 25 '14 at 04:49
  • I DOnt want to show the systemfiles and folders in my file picker.. This code allows me to pick the file but it also shows me the systemfolders of android which are starting from "."(dot) which i want to hide from the user – Kirtesh Jul 25 '14 at 06:35
  • Then, I don't understand what the problem is. If you don't want to show a file/folder, don't add it to your list. – Mike M. Jul 25 '14 at 06:40
  • Means I dint get you?? I already tried this `chkSystemFolder =String.valueOf(chkFileName.charAt(0)); if(chkSystemFolder != ".")` – Kirtesh Jul 25 '14 at 06:41
  • Thats somewhere i m going wrong i know I m just not getting where to add this code which result me to what i need – Kirtesh Jul 25 '14 at 06:43
  • Add the following as the first line in the `for` loop in your `fill()` method: `if(ff.isHidden()) continue;` – Mike M. Jul 25 '14 at 07:15
  • You mean like this? `for(File ff: dirs) { if(ff.isHidden()) continue; Date lastModDate = new Date(ff.lastModified()); DateFormat formater = DateFormat.getDateTimeInstance();` – Kirtesh Jul 25 '14 at 07:26
  • I dont know why but u r code really helps me ty so much :) – Kirtesh Jul 25 '14 at 07:45
  • I'll put the explanation in an answer. – Mike M. Jul 25 '14 at 07:49

1 Answers1

1

Add the following as the first line in the for loop of your fill() method:

 if(ff.isHidden()) continue;

The File#isHidden() returns true if the file or folder it represents is hidden; i.e. if its filename starts with '.'. The continue statement inside a loop causes the current iteration of the loop to be skipped. So, this code basically says "If this file/folder is hidden, just go to the next one."

Mike M.
  • 38,532
  • 8
  • 99
  • 95