我有一个简单的文件管理器。在我浏览文件时的目录中,它只显示一些特定的文件扩展名,例如.txt和.pdf。它还将显示目录。我该怎么做。应该添加或修改以下代码。 FileManager.java文件是 -
package com.radiobot.speedreaderv1_1;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FileManager extends ListActivity {
private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; }
private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
private List<String> directoryEntries = new ArrayList<String>();
private File currentDirectory = new File("/");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
browseToRoot();
}
private void browseToRoot() {
browseTo(new File("/"));
}
private void upOneLevel(){
if(this.currentDirectory.getParent() != null)
this.browseTo(this.currentDirectory.getParentFile());
}
private void browseTo(final File aDirectory){
if (aDirectory.isDirectory()){
this.currentDirectory = aDirectory;
fill(aDirectory.listFiles());
}
else{
OnClickListener okButtonListener = new OnClickListener(){
// @Override
public void onClick(DialogInterface arg0, int arg1) {
{
try
{
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
File file = new File(aDirectory.getAbsolutePath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
myIntent.setDataAndType(Uri.fromFile(file),mimetype);
startActivity(myIntent);
}
catch (Exception e)
{
e.getMessage();
}
}
}
};
OnClickListener cancelButtonListener = new OnClickListener(){
// @Override
public void onClick(DialogInterface arg0, int arg1) {
// Do nothing
}
};
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle(
"Do you want to open this file" + aDirectory.getName())
.setPositiveButton("OK", okButtonListener)
.setNegativeButton("Cancel", cancelButtonListener)
.show();
}
}
private void fill(File[] files) {
this.directoryEntries.clear();
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.directoryEntries.add("");
if(this.currentDirectory.getParent() != null)
this.directoryEntries.add("..");
switch(this.displayMode){
case ABSOLUTE:
for (File file : files){
this.directoryEntries.add(file.getPath());
}
break;
case RELATIVE:
int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
for (File file : files){
this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
}
break;
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
R.layout.filemanager, this.directoryEntries);
this.setListAdapter(directoryList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String position2 = (String) l.getItemAtPosition(position);
String selectedFileString = position2;
if (selectedFileString.equals(".")) {
// Refresh
this.browseTo(this.currentDirectory);
} else if(selectedFileString.equals("..")){
this.upOneLevel();
} else {
File clickedFile = null;
switch(this.displayMode){
case RELATIVE:
clickedFile = new File(position2);
break;
case ABSOLUTE:
clickedFile = new File(position2);
break;
}
if(clickedFile != null)
this.browseTo(clickedFile);
}
}
}
答案 0 :(得分:0)
FileFilter filter = new FileNameExtensionFilter(“JPEG file”,“jpg”, “JPEG”); JFileChooser fileChooser = ...; fileChooser.addChoosableFileFilter(过滤器);
答案 1 :(得分:0)
在browseTo()
中,您将目录中的所有文件添加到列表中:
fill(aDirectory.listFiles()); // here's the problem
查看File.listFiles(FileFilter filter)
,这只返回符合指定过滤器的文件。
File[] files = aDirectory.listFiles(new FileFilter() {
@Override
boolean accept(File pathname) {
// return true if and only if pathname has the desired extension
if (pathname.getName().endsWith(".txt") || pathname.isDirectory())
return true; // accept only .txt files and directories
else
return false;
}
});