Created
August 9, 2016 20:40
-
-
Save nathanfletcher/bbc999dc663b58c5e42b6802c25aad76 to your computer and use it in GitHub Desktop.
This is a simple file chooser class for android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.Dialog; | |
import android.os.Environment; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.WindowManager.LayoutParams; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import java.io.File; | |
import java.io.FileFilter; | |
import java.util.Arrays; | |
public class FileChooser { | |
private static final String PARENT_DIR = ".."; | |
private final Activity activity; | |
private ListView list; | |
private Dialog dialog; | |
private File currentPath; | |
// filter on file extension | |
private String extension = null; | |
public void setExtension(String extension) { | |
this.extension = (extension == null) ? null : | |
extension.toLowerCase(); | |
} | |
// file selection event handling | |
public interface FileSelectedListener { | |
void fileSelected(File file); | |
} | |
public FileChooser setFileListener(FileSelectedListener fileListener) { | |
this.fileListener = fileListener; | |
return this; | |
} | |
private FileSelectedListener fileListener; | |
public FileChooser(Activity activity) { | |
this.activity = activity; | |
dialog = new Dialog(activity); | |
list = new ListView(activity); | |
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override public void onItemClick(AdapterView<?> parent, View view, int which, long id) { | |
String fileChosen = (String) list.getItemAtPosition(which); | |
File chosenFile = getChosenFile(fileChosen); | |
if (chosenFile.isDirectory()) { | |
refresh(chosenFile); | |
} else { | |
if (fileListener != null) { | |
fileListener.fileSelected(chosenFile); | |
} | |
dialog.dismiss(); | |
} | |
} | |
}); | |
dialog.setContentView(list); | |
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); | |
refresh(Environment.getExternalStorageDirectory()); | |
} | |
public void showDialog() { | |
dialog.show(); | |
} | |
/** | |
* Sort, filter and display the files for the given path. | |
*/ | |
private void refresh(File path) { | |
this.currentPath = path; | |
if (path.exists()) { | |
File[] dirs = path.listFiles(new FileFilter() { | |
@Override public boolean accept(File file) { | |
return (file.isDirectory() && file.canRead()); | |
} | |
}); | |
File[] files = path.listFiles(new FileFilter() { | |
@Override public boolean accept(File file) { | |
if (!file.isDirectory()) { | |
if (!file.canRead()) { | |
return false; | |
} else if (extension == null) { | |
return true; | |
} else { | |
return file.getName().toLowerCase().endsWith(extension); | |
} | |
} else { | |
return false; | |
} | |
} | |
}); | |
// convert to an array | |
int i = 0; | |
String[] fileList; | |
if (path.getParentFile() == null) { | |
fileList = new String[dirs.length + files.length]; | |
} else { | |
fileList = new String[dirs.length + files.length + 1]; | |
fileList[i++] = PARENT_DIR; | |
} | |
Arrays.sort(dirs); | |
Arrays.sort(files); | |
for (File dir : dirs) { fileList[i++] = dir.getName(); } | |
for (File file : files ) { fileList[i++] = file.getName(); } | |
// refresh the user interface | |
dialog.setTitle(currentPath.getPath()); | |
list.setAdapter(new ArrayAdapter(activity, | |
android.R.layout.simple_list_item_1, fileList) { | |
@Override public View getView(int pos, View view, ViewGroup parent) { | |
view = super.getView(pos, view, parent); | |
((TextView) view).setSingleLine(true); | |
return view; | |
} | |
}); | |
} | |
} | |
/** | |
* Convert a relative filename into an actual File object. | |
*/ | |
private File getChosenFile(String fileChosen) { | |
if (fileChosen.equals(PARENT_DIR)) { | |
return currentPath.getParentFile(); | |
} else { | |
return new File(currentPath, fileChosen); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//...ignoring other code.../ | |
/*Call this method whenever you want to show the file chooser*/ | |
/** | |
* Tweak method from website*/ | |
private void processFile(){ | |
FileChooser fileChooser = new FileChooser(MainActivity.this); | |
fileChooser.setFileListener(new FileChooser.FileSelectedListener() { | |
@Override | |
public void fileSelected(final File file) { | |
// ....do something with the file | |
String filename = file.getAbsolutePath(); | |
Log.i("File Name", filename); | |
// then actually do something in another module | |
} | |
}); | |
// Set up and filter my extension I am looking for | |
//fileChooser.setExtension("pdf"); | |
fileChooser.showDialog(); | |
} | |
/*According to documentation from Roger Keays*/ | |
/*new FileChooser(activity).setFileListener(new FileSelectedListener() { | |
@Override public void fileSelected(final File file) { | |
// do something with the file | |
}).showDialog();*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Environment.getExternalStorageDirectory() is deprecated. Does anybody know how to access the root directory now?