Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all files and folders locating on sd card

I have made a program that list all files and folders(f&f) locating on sd card. If i touch one of the list item ( if it is a folder ) then the list shows faf locating on that folder.

Here is the source code

public class FileList extends ListActivity 
{
private File file;
private List<String> myList;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    myList = new ArrayList<String>();   

    String root_sd = Environment.getExternalStorageDirectory().toString();
    file = new File( root_sd + "/external_sd" ) ;       
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
        myList.add( list[i].getName() );
    }

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList ));

}

protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);

    File temp_file = new File( file, myList.get( position ) );  

    if( !temp_file.isFile())        
    {
        file = new File( file, myList.get( position ));
        File list[] = file.listFiles();

        myList.clear();

        for( int i=0; i< list.length; i++)
        {
            myList.add( list[i].getName() );
        }
        Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myList ));

    }

}


@Override
public boolean dispatchKeyEvent(KeyEvent event) 
{
    if( KeyEvent.KEYCODE_BACK == event.getKeyCode())
    {
        String parent = file.getParent().toString();
        file = new File( parent ) ;         
        File list[] = file.listFiles();

        myList.clear();

        for( int i=0; i< list.length; i++)
        {
            myList.add( list[i].getName() );
        }
        Toast.makeText(getApplicationContext(), parent,          Toast.LENGTH_LONG).show(); 
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myList ));


    }

    return true;
}   

}

Now i have two questions

  1. when i touch "back" button then it list back two step. say currently the list is showing the f&f under "external_sd/Video/Bangla" . After pressing back button the list is not showing f&f under "external_sd/Video/", but under "external_sd/".

  2. Is there a better solution to show all f&f like JFileChooser in java ?

like image 214
Imdad Sarkar Avatar asked Dec 26 '11 18:12

Imdad Sarkar


4 Answers

It seems that when you touch Back dispatchKeyEvent() receive twice the KeyEvent KEYCODE_BACK, so I suggest you do it this way :

public class FileList extends ListActivity 
{
private File file;
private List<String> myList;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    myList = new ArrayList<String>();   

    String root_sd = Environment.getExternalStorageDirectory().toString();
    file = new File( root_sd + "/external_sd" ) ;       
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            myList.add( list[i].getName() );
    }

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList ));

}

protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);

    File temp_file = new File( file, myList.get( position ) );  

    if( !temp_file.isFile())        
    {
        file = new File( file, myList.get( position ));
        File list[] = file.listFiles();

        myList.clear();

        for( int i=0; i< list.length; i++)
        {
            myList.add( list[i].getName() );
        }
        Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myList ));

    }

}


@Override
public void onBackPressed() {
            String parent = file.getParent().toString();
            file = new File( parent ) ;         
            File list[] = file.listFiles();

            myList.clear();

            for( int i=0; i< list.length; i++)
            {
                myList.add( list[i].getName() );
            }
            Toast.makeText(getApplicationContext(), parent,          Toast.LENGTH_LONG).show(); 
            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, myList ));


    }
like image 105
Rick Avatar answered Oct 08 '22 15:10

Rick


To list all the files and folders of the External Storage, it is better if you use getExternalStorageDirectory():

final String state = Environment.getExternalStorageState();

if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) {  // we can read the External Storage...
    getAllFilesOfDir(Environment.getExternalStorageDirectory());
}

Where the getAllFilesOfDir() is the recoursive method:

private void getAllFilesOfDir(File directory) {
    Log.d(TAG, "Directory: " + directory.getAbsolutePath() + "\n");

    final File[] files = directory.listFiles();

    if ( files != null ) {
        for ( File file : files ) {
            if ( file != null ) {
                if ( file.isDirectory() ) {  // it is a folder...
                    getAllFilesOfDir(file);
                }
                else {  // it is a file...
                    Log.d(TAG, "File: " + file.getAbsolutePath() + "\n");
                }
            }
        }
    }
}
like image 37
Paolo Rovelli Avatar answered Oct 08 '22 17:10

Paolo Rovelli


ArrayList<File> fileList = new ArrayList<File>();
public ArrayList<File> getfile(File dir,String fileType)//pass fileType as a music , video, etc.               
{
    File listFile[] = dir.listFiles();
    if (listFile != null && listFile.length > 0) 
    {
        for (int i = 0; i < listFile.length; i++) 
        {

            if (listFile[i].isDirectory()) 
            {
                getfile(listFile[i],fileType);

            } 
            else 
            {
                if("doc".equals(fileType))
                {
                    if(listFile[i].getName().endsWith(".pdf") || listFile[i].getName().endsWith(".txt") || 
                       listFile[i].getName().endsWith(".xml") || listFile[i].getName().endsWith(".doc") ||
                       listFile[i].getName().endsWith(".xls") || listFile[i].getName().endsWith(".xlsx"))
                     {
                        fileList.add(listFile[i]);  
                     }
                }
                else if("music".equals(fileType))
                {
                    if(listFile[i].getName().endsWith(".mp3"))
                    {
                        fileList.add(listFile[i]);
                    }
                }
                else if("video".equals(fileType))
                {
                    if(listFile[i].getName().endsWith(".mp4"))
                    {
                        fileList.add(listFile[i]);
                    }
                }
                else if("image".equals(fileType))
                {
                    if(listFile[i].getName().endsWith(".png") || listFile[i].getName().endsWith(".jpg")
                       || listFile[i].getName().endsWith(".jpeg") || listFile[i].getName().endsWith(".gif"))
                    {
                        fileList.add(listFile[i]);
                    }
                }
            }
        }
    }
    return fileList;
}
like image 1
Vikrant Avatar answered Oct 08 '22 17:10

Vikrant


By this for txt files:

    public class TXTList extends Activity {

    ArrayList<File> allTXT = new ArrayList<File>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        searchTXT(new File(Environment.getExternalStorageDirectory().getPath()));

    }

    private void searchTXT(File dir){
        File[] files = dir.listFiles();
//      Log.i("DIR", "Found " + files.length + " in " + dir.getAbsolutePath());
        for (File file : files) {
            if(file.isFile() && isTXT(file)){
                allTXT.add(file);
                Log.i("TXT", file.getName());
            }else if(file.isDirectory()){
                searchTXT(file.getAbsoluteFile()); 
            }
        }
    }

    private boolean isTXT(File file){
        boolean is = false;
        if(file.getName().endsWith(".txt")){
            is = true;
        }
        return is;
    }

}
like image 1
Omid Omidi Avatar answered Oct 08 '22 17:10

Omid Omidi