File Browser in Andriod

File browser in android is used to access the internal file system in Android. Through the file browser, we can browse through the files. The features are

- sorting,
- size of the file
- an icon to recognize the file or directory
- open the file, if permission is there.

Then a Home button, to come to the root directory, SDcard button, to show sdcard files. A back button is always there, if the parent directory is not null.

For opening the file, there is function, I created called openFile(), in that, according to the extension of the file, I am setting the MIME type for intent using the API called setDataAndType(). The android file system contains lot of file types like audio, video, text, (text itself contain html, php, xml, csv etc).

Another important method(API), is listFiles(). This will return an array of files and this array, we are passing to the fill() in the FileBrowser class. We will check a file is a directory or not using isDirectory()


The code for openFile() which is an very important function in the fileBrowser application and can be used in several applications is,


 

private void openFile(File aFile) {
Log.i("openFile()", "openFile()");
String type = "";
String extension = "";
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file://" + aFile.getAbsolutePath());
int dot = aFile.getName().lastIndexOf(".");
extension = aFile.getName().substring(dot);
type = extension.toLowerCase();
// audio-types
if (type.equals(".ogg"))
type = "audio/x-ogg";
if (type.equals(".mp3"))
type = "audio/mp3";
if (type.equals(".wav"))
type = "audio/wav";
if (type.equals(".mid"))
type = "audio/mid";
if (type.equals(".midi"))
type = "audio/midi";
if (type.equals(".amr"))
type = "audio/AMR";
// video-types
if (type.equals(".mpeg"))
type = "video/mpeg";
if (type.equals("..3gp"))
type = "video/3gpp";
// package-types
if (type.equals(".jar"))
type = "application/java-archive";
if (type.equals(".zip"))
type = "application/zip";
if (type.equals(".rar"))
type = "application/x-rar-compressed";
if (type.equals(".gz"))
type = "application/gzip";
// android-specific
if (type.equals(".apk"))
type = "application/vnd.android.package-archive";
// web browser-types
if (type.equals(".htm"))
type = "text/html";
if (type.equals(".html"))
type = "text/html";
if (type.equals(".php"))
type = "text/php";
// text-types
if (type.equals(".txt"))
type = "text/plain";
if (type.equals(".csv"))
type = "text/csv";
if (type.equals(".xml"))
type = "text/";
// image-types
if (type.equals(".png"))
type = "image/png";
if (type.equals(".gif"))
type = "image/gif";
if (type.equals(".jpg"))
type = "image/jpg";
if (type.equals(".jpeg"))
type = "image/jpeg";
if (type.equals(".bmp"))
type = "image/bmp";

intent.setDataAndType(data, type);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(this, "Protected, File can't open",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}


The complete code snippet is,

 
public class FileBrowser extends ListActivity {

private enum DISPLAYMODE {
ABSOLUTE, RELATIVE;
}

static boolean SORT_NAME = false;
static boolean SMALL_ICON = false;
private final DISPLAYMODE displayMode = DISPLAYMODE.RELATIVE;
private static List directoryEntries = new ArrayList();
private File currentDirectory = new File("");
private TextView fullPath;
private File[] filesToFill = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fileui);
fullPath = (TextView) findViewById(R.id.path);
findViewById(R.id.home).setOnClickListener(new OnClickListener() {

public void onClick(View view) {
Log.i("onClick, HomeButton", "onClick, HomeButton");
browseToRoot();
}
});
findViewById(R.id.sdcard).setOnClickListener(new OnClickListener() {


public void onClick(View view) {
browseTo(new File("/sdcard"));
}

});
browseToRoot();
}

private void browseToRoot() {
browseTo(new File("/"));
}

private void upOneLevel() {
if (this.currentDirectory.getParent() != null) {
this.browseTo(this.currentDirectory.getParentFile());
} else {
Toast.makeText(this, "FileBrowser can't go Up", Toast.LENGTH_SHORT)
.show();
}
}

private void browseTo(final File aDirectory) {

if (this.displayMode == DISPLAYMODE.RELATIVE) {
Log.i("browseTo(), DISPLAYMODE_RELATIVE, absPath : "
+ aDirectory.getAbsolutePath(), "DISPLAYMODE_RELATIVE : ");
fullPath.setText("CurrentPath :" + aDirectory.getAbsolutePath());
}
if (aDirectory.isDirectory()) {
Log.i("browseTo(), isDir() == true", "browseTo(), isDir() == true");
this.currentDirectory = aDirectory;
fill(aDirectory.listFiles());
} else {
Log.i("browseTo(), isDir() != true", "browseTo(), isDir() != true");
openFile(aDirectory);
}
}

private void openFile(File aFile) {
Log.i("openFile()", "openFile()");
String type = "";
String extension = "";
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file://" + aFile.getAbsolutePath());
int dot = aFile.getName().lastIndexOf(".");
extension = aFile.getName().substring(dot);
type = extension.toLowerCase();
// audio-types
if (type.equals(".ogg"))
type = "audio/x-ogg";
if (type.equals(".mp3"))
type = "audio/mp3";
if (type.equals(".wav"))
type = "audio/wav";
if (type.equals(".mid"))
type = "audio/mid";
if (type.equals(".midi"))
type = "audio/midi";
if (type.equals(".amr"))
type = "audio/AMR";
// video-types
if (type.equals(".mpeg"))
type = "video/mpeg";
if (type.equals("..3gp"))
type = "video/3gpp";
// package-types
if (type.equals(".jar"))
type = "application/java-archive";
if (type.equals(".zip"))
type = "application/zip";
if (type.equals(".rar"))
type = "application/x-rar-compressed";
if (type.equals(".gz"))
type = "application/gzip";
// android-specific
if (type.equals(".apk"))
type = "application/vnd.android.package-archive";
// web browser-types
if (type.equals(".htm"))
type = "text/html";
if (type.equals(".html"))
type = "text/html";
if (type.equals(".php"))
type = "text/php";
// text-types
if (type.equals(".txt"))
type = "text/plain";
if (type.equals(".csv"))
type = "text/csv";
if (type.equals(".xml"))
type = "text/";
// image-types
if (type.equals(".png"))
type = "image/png";
if (type.equals(".gif"))
type = "image/gif";
if (type.equals(".jpg"))
type = "image/jpg";
if (type.equals(".jpeg"))
type = "image/jpeg";
if (type.equals(".bmp"))
type = "image/bmp";

intent.setDataAndType(data, type);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(this, "Protected, File can't open",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}

private void fill(File[] files) {
Log.i("in fill(), from browseTo()", "in fill(), from browseTo()");
FileBrowser.directoryEntries.clear();
filesToFill = files;

if (this.currentDirectory.getParent() != null) {
FileBrowser.directoryEntries.add(new IconifiedText("Back",
getResources().getDrawable(R.drawable.ic_menu_back_small)));
}
Drawable currentIcon = null;
for (File currentFile : files) {
String fileName = "";
Log.i("currentFile : Length :" + currentFile.length() + "Bytes",
"Size of currentFile");
if (currentFile.isDirectory()) {
currentIcon = getResources().getDrawable(R.drawable.folder3);
} else {
// String fileName = currentFile.getName();
// if (checkEndsWithInStringArray(fileName, getResources()
// .getStringArray(R.array.fileEndingImage))) {
// // currentIcon =
// // getResources().getDrawable(R.drawable.image);
// } else if (checkEndsWithInStringArray(fileName,
currentIcon = getResources().getDrawable(R.drawable.file);

}
switch (this.displayMode) {
case ABSOLUTE:
/* On absolute Mode, we show the full path */
FileBrowser.directoryEntries.add(new IconifiedText(currentFile
.getPath(), currentIcon));
break;
case RELATIVE:
int currentPathStringLength = this.currentDirectory.getPath()
.length();
fileName = currentFile.getAbsolutePath().substring(
currentPathStringLength);
Log.i("else part :" + fileName, "else part");
break;
}

if (FileBrowser.SMALL_ICON) {
if (currentFile.isDirectory())
currentIcon = getResources().getDrawable(R.drawable.folder);
else
currentIcon = getResources()
.getDrawable(R.drawable.webtext);
}
FileBrowser.directoryEntries.add(new IconifiedText(fileName,
currentIcon));

}

if (FileBrowser.SORT_NAME) {
Collections.sort(FileBrowser.directoryEntries);
}
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
itla.setListItems(FileBrowser.directoryEntries);
this.setListAdapter(itla);
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String selectedFileString = FileBrowser.directoryEntries.get(position)
.getText();
Log.i("selectedItem : " + selectedFileString, "onListItemClick()");
if (selectedFileString.equals("Home")) {
// Refresh
// this.browseTo(this.currentDirectory);
browseToRoot();
} else if (selectedFileString.equals("Back")) {
this.upOneLevel();
} else if (selectedFileString.equals("data")) {
this.browseTo(new File("/data/misc/"));
} else {
File clickedFile = null;
switch (this.displayMode) {
case RELATIVE:
clickedFile = new File(this.currentDirectory.getAbsolutePath()
+ FileBrowser.directoryEntries.get(position).getText());
break;
case ABSOLUTE:
clickedFile = new File(FileBrowser.directoryEntries.get(
position).getText());
Log.i("ABSOLUTE", "ABSOLUTE");
break;
}
if (clickedFile != null) {
this.browseTo(clickedFile);
}
}
}



This itself is not complete, other class like IconifiedText, Adapter class , xml files are there.

The screenshots are,



Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Difference between SurfaceView and View in Android