How to List All Files and Folders Locating on Sd Card

How to list all files and folders locating on sd card

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

}

List all text files present on SD card

Use .endsWith() method from Java String Class to check File Extension from file path.

Method:

public boolean endsWith(String suffix)

Your Code something like,

private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("your path");

File[] files=f.listFiles();

for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
String filePath = file.getPath();
if(filePath.endsWith(".txt")) // Condition to check .txt file extension
tFileList.add(filePath);
}
return tFileList;
}

What is a good way to get a list of files from (a directory on) the sd card?

final String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File[] files = Environment.getExternalStorageDirectory().listFiles();
} else {
...
}

Retrieve all video files from all directories of SD card

You can create a list which can store the location of all the video files present on sd card. Run a loop which will visit every folder and update this array if given file format (in your case video files or .mp4) and add it to array. You can store this list onto persistent storage so as you can read it the next time your application is launched.

Here is sample code which can help you list all files in sdcard

public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
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;
}

Retrieve lists of file in a directory

If you want to retrieve files from the external storage, it is easiest to work with java Files, Start by getting the sdcard path, using the Environment-class:

String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();

sdcard is now a String containing the file path to the sd-card (usually '/sdcard/' I think).

Add your own path to this base path, to get the directory you want to access:

String myfilepath = sdcard+"folder/";

To list the files in this directory you can create a file from the path:

File dir = new File(myfilepath);

And then finally, get a list of the files in that directory:

dir.listFiles(); //Returns a File[]
dir.list(); //Returns a String[] with filenames

As mentioned, do some searching next time before asking a question.



Related Topics



Leave a reply



Submit