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
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");
}
}
}
}
}
A simple example
File Lis=new File("FolderPatch");
Lis.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
Log.e(">>>>", filename+"<<<");
return false;
}
});
import java.io.*;
import java.util.*;
public class DirUtils {
public static List recurseDir(String dir) {
String result, _result[];
result = recurseInDirFrom(dir);
_result = result.split("\\|");
return Arrays.asList(_result);
}
private static String recurseInDirFrom(String dirItem) {
File file;
String result,list[];
result = dirItem;
file = new File(dirItem);
if (file.isDirectory()) {
list = file.list();
File[] fileslist = file.listFiles(new MyDocFileFilter());
if (fileslist != null) {
for (File file1: fileslist) {
System.out.println(file1.getAbsolutePath());
}
}
else {
System.out.println("No Subdirectory Found.");
}
for (int i = 0; i < list.length; i++)
result = result + "\n" + recurseInDirFrom(dirItem + File.separatorChar + list[i]);
}
return result;
}
static class MyDocFileFilter implements FileFilter
{
private final String[] myDocumentExtensions
= new String[] {".java", ".png", ".html", "class"};
public boolean accept(File file) {
if (!file.isFile())
return false;
for (String extension : myDocumentExtensions) {
if (file.getName().toLowerCase().endsWith(extension))
return true;
}
return false;
}
}
public static void main(String arg[]) {
DirUtils.recurseDir("your path ");
}
}
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 ));
}
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;
}
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;
}
}