Saturday, June 4, 2011

Creating a File Browser in Android

The Connexions app can be used to download PDF and EPUB files so it was a natural to allow the user to view the downloaded files, select one and do something with it. The code for most of this functionality is in the FileBrowserActivity class. The readFileList() method is called by onCreate(). readFileList() checks to see if the Connexions directory exists. If it does, the user has downloaded a PDF or EPUB, so it calls handleFIle().


public void readFileList()
{
currentDirectory = new File(Environment.getExternalStorageDirectory(), "Connexions/");
if(currentDirectory.exists())
{
handleFile(currentDirectory);
}

}



The method handleFile() checks if the file/directory passed in is the Connexions directory. If it is, the list of files is loaded into a List object. It is the behavior when onCreate() calls the method. If a user selects a file from the displayed list, the onListItemClick() method calls handleFile() which opens an alert to ask if the user wants to open the file.


private void handleFile(final File dirOrFile)
{
//Log.d("FileBrowserActivity.browseTo()", "Called");
if (dirOrFile.isDirectory() && !dirOrFile.getPath().endsWith("Connexions/"))
{
this.currentDirectory = dirOrFile;
loadList(dirOrFile.listFiles());
}
else
{

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(getString(R.string.file_dialog_title));
alertDialog.setMessage("Open file " + dirOrFile.getName() + "?");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
openFile(dirOrFile);

} });
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//do nothing

} });
alertDialog.show();

}
}


If the user selects to open the file, openFile() is called and it tries to open the file in a different application for the file mime-type. If there is no app for the mime-type, a Toast message is displayed to notify the user.


private void openFile(File file)
{

File newFile = new File(Environment.getExternalStorageDirectory() + "/Connexions/" + file.getName());
Uri path = Uri.fromFile(newFile);
//Log.d("FileBrowserActivity", "path: " + path.toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(file.getAbsolutePath().indexOf(".pdf") > -1)
{
intent.setDataAndType(path, "application/pdf");
}
else if(file.getAbsolutePath().indexOf(".epub") > -1)
{
intent.setDataAndType(path, "application/epub+zip");
}

try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(FileBrowserActivity.this, getString(R.string.file_browser_toast), Toast.LENGTH_SHORT).show();
}
}


As always, you can browse the source or download a zip file of the source. Connexions for Android is available in the Android Market or from the Connexions website.

No comments:

Post a Comment