我制作了一个程序,列出了位于SD卡上的所有文件和文件夹(f& f)。如果我触摸其中一个列表项 (如果它是一个文件夹)然后列表显示faf定位在该文件夹上。
这是源代码
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;
}
}
现在我有两个问题
当我触摸“后退”按钮时,它会返回两步。说当前列表显示“external_sd / Video / Bangla”下的f&amp; f。 按下后退按钮后,列表不会在“external_sd / Video /”下显示f&amp; f,而是在“external_sd /".
是否有更好的解决方案可以在java中显示JFileChooser等所有f&amp; f?
答案 0 :(得分:29)
当你触摸后,dispatchKeyEvent()似乎会收到两次KeyEvent KEYCODE_BACK,所以我建议你这样做:
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 ));
}
答案 1 :(得分:6)
要列出外部存储的所有文件和文件夹,最好使用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());
}
getAllFilesOfDir()是reoursive方法:
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");
}
}
}
}
}
答案 2 :(得分:1)
通过这个用于txt文件:
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;
}
}
答案 3 :(得分:0)
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 ");
}
}
答案 4 :(得分:0)
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;
}
答案 5 :(得分:0)
一个简单的例子
File Lis=new File("FolderPatch");
Lis.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
Log.e(">>>>", filename+"<<<");
return false;
}
});