Oct 11, 2011

Navigation in files/directory

Navigation in files/directory

package com.AndroidListFiles;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidListFilesActivity extends ListActivity {

private List<String> fileList = new ArrayList<String>();

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

File root = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
ListDir(root);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
}else {
Toast.makeText(AndroidListFilesActivity.this,
selected.toString() + " selected",
Toast.LENGTH_LONG).show();
}
}

void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getPath());
}

ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}

}


prev:
- List files/directory in Android

next:
- Get File Extension and MIME Type



1 comment:

  1. Very nice and simple code !! It is really the best I've seen around !! Thanks alot !!

    ReplyDelete

Infolinks In Text Ads