Simple File Dialog For Android Applications

Since there isn’t a built in file dialog interface for android development I have been searching for one that I could use without starting from scratch. I did eventually find a Simple Directory Chooser with a very lenient license (The Code Project Open License (CPOL) 1.02). The directory chooser I found uses a dialog interface so there is no need to create an additional activity to use it.

The directory chooser was great but I needed a file dialog for choosing files for opening and saving not a directory chooser. To get the file dialog I needed I modified the original directory chooser adding to it as needed to create a Simple File Dialog for my own use. I thought I would post my modified version here for others to use or build on.

The file dialog code consists of a single file and uses standard Android resources. This file dialog code should be very portable and easily inserted into any project to add menu driven file selection within Android.  Screen shots of the three file dialog modes (Open, Save, and Folder Select) are shown below. The “Save” and “Folder Select” modes include a button for adding a new folder to the file structure.

Open Dialog save folder

The code can be downloaded here: SimpleFileDialog.java and MainActivity.java

Bare Bones Application Using the Simple File Dialog

This is a very bare bones application with three buttons that call the Simple File Dialog in all three modes (Save, Open and Folder Select). To make this application I just started a new Android Application Project in Eclipse. Edited the Layout to add three buttons and edited the MainActivity.java file. The finished MainActivity.java file is shown below.

Demo Screen

//MainActivity.java
package com.scorchworks.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;

//import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//Button1
		Button dirChooserButton1 = (Button) findViewById(R.id.button1);
		dirChooserButton1.setOnClickListener(new OnClickListener() 
		{
			String m_chosen;
			@Override
			public void onClick(View v) {
				/////////////////////////////////////////////////////////////////////////////////////////////////
				//Create FileOpenDialog and register a callback
				/////////////////////////////////////////////////////////////////////////////////////////////////
				SimpleFileDialog FileOpenDialog =  new SimpleFileDialog(MainActivity.this, "FileOpen",
						new SimpleFileDialog.SimpleFileDialogListener()
				{
					@Override
					public void onChosenDir(String chosenDir) 
					{
						// The code in this function will be executed when the dialog OK button is pushed 
						m_chosen = chosenDir;
						Toast.makeText(MainActivity.this, "Chosen FileOpenDialog File: " + 
								m_chosen, Toast.LENGTH_LONG).show();
					}
				});

				//You can change the default filename using the public variable "Default_File_Name"
				FileOpenDialog.Default_File_Name = "";
				FileOpenDialog.chooseFile_or_Dir();

				/////////////////////////////////////////////////////////////////////////////////////////////////

			}
		});

		//Button2
		Button dirChooserButton2 = (Button) findViewById(R.id.button2);
		dirChooserButton2.setOnClickListener(new OnClickListener() 
		{
			String m_chosen;
			@Override
			public void onClick(View v) {
				/////////////////////////////////////////////////////////////////////////////////////////////////
				//Create FileSaveDialog and register a callback
				/////////////////////////////////////////////////////////////////////////////////////////////////
				SimpleFileDialog FileSaveDialog =  new SimpleFileDialog(MainActivity.this, "FileSave",
						new SimpleFileDialog.SimpleFileDialogListener()
				{
					@Override
					public void onChosenDir(String chosenDir) 
					{
						// The code in this function will be executed when the dialog OK button is pushed
						m_chosen = chosenDir;
						Toast.makeText(MainActivity.this, "Chosen FileOpenDialog File: " + 
								m_chosen, Toast.LENGTH_LONG).show();
					}
				});

				//You can change the default filename using the public variable "Default_File_Name"
				FileSaveDialog.Default_File_Name = "my_default.txt";
				FileSaveDialog.chooseFile_or_Dir();

				/////////////////////////////////////////////////////////////////////////////////////////////////

			}
		});

		//Button3
		Button dirChooserButton3 = (Button) findViewById(R.id.button3);
		dirChooserButton3.setOnClickListener(new OnClickListener() 
		{
			String m_chosen;
			@Override
			public void onClick(View v) {

				/////////////////////////////////////////////////////////////////////////////////////////////////
				//Create FileOpenDialog and register a callback
				/////////////////////////////////////////////////////////////////////////////////////////////////
				SimpleFileDialog FolderChooseDialog =  new SimpleFileDialog(MainActivity.this, "FolderChoose",
						new SimpleFileDialog.SimpleFileDialogListener()
				{
					@Override
					public void onChosenDir(String chosenDir) 
					{
						// The code in this function will be executed when the dialog OK button is pushed
						m_chosen = chosenDir;
						Toast.makeText(MainActivity.this, "Chosen FileOpenDialog File: " + 
								m_chosen, Toast.LENGTH_LONG).show();
					}
				});

				FolderChooseDialog.chooseFile_or_Dir();

				/////////////////////////////////////////////////////////////////////////////////////////////////

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
//

For the New Folder button to actually create a new folder in the demo you need to include the following line in the Manifest file to give the app permission to write to the SD Card.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

None of this code would have been possible without the jump start I got from the Simple Directory Chooser

2 thoughts on “Simple File Dialog For Android Applications

Leave a Reply