Android embedded file browser

This tutorial is about adding a file browser into your Android application.
After this tutorial, your application will be independent! No need for external file browsers! Setting this up will only take you 10 minutes!

Screenshot_2014-04-06-19-50-11

To get started, you will need to download the used library. Paste the next command into your terminal:
svn checkout http://android-file-dialog.googlecode.com/svn/trunk/ android-file-dialog-read-only

Import this project into your eclipse, and do not forget to chose the copy into workspace option!

Creating a JAR from your library project
Your Android application project needs a jar from the library you want to add.

In your eclipse, right click on the android file dialog project again and chose “properties”. Go to “Android” and check the “Is library”. Make sure it’s checked, else it wont work. Now right click on the file dialog project again and go to “export”. Select in the menu “Java” and chose for “JAR file”.

Open the android file dialog project and select the “src” folder. No other folders are needed. As “export destination” , go to the android file dialog project directory and put it in the “bin” folder.

Click the “Finish” button.

The JAR file is created in the bin directory.
Now it’s time to import the library into your project.

Right click on your project and chose for “properties”. Pick “Android” from the menu and click “add”. Select the android file dialog project and click “Ok”.

Right click on your project again and chose for “Build Path” and “Configure Build Path”. Go to the “Libraries” tab and select “Add JARs”. Browse to the Android File Dialog project and select the created JAR file. Click “Ok” and we are done!

Using the Library.

In this paragraph I am going to post some ready to use examples, very easy to implement into your own project.

Calling the file browser
Intent intent = new Intent(getBaseContext(), FileDialog.class);
intent.putExtra(FileDialog.START_PATH, "/sdcard");
//can user select directories or not
intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
//alternatively you can set file filter
//intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "png" });
startActivityForResult(intent, REQUEST_SAVE);

It opens a file browser, starting in the /sdcard directory. You can change it to anywhere you like. Thre REQUEST_SAVE is the integer constant that will be returned to the onActivityResult function. The first argument will be the value of the REQUEST_SAVE integer. The integer has to be created global!

Saving or loading a file selected from the file browser

Inside the onActivityResult function, you can use this to save and load a file:

This is a snippet, to show what’s in the source* you can download below. If you want to use this, check out the sources*, it also contains a read and write function!

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode=REQUEST_SAVE){
String filePath = data.getStringExtra(FileDialog.RESULT_PATH);
try{
File CodeFile =new File(filePath);
//if file doesnt exists, then create it
if(!CodeFile.exists()){
CodeFile.createNewFile();
}
FileWriter fileWritter = new FileWriter(CodeFile,true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write("Your new text.");
bufferWritter.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
if(requestCode == REQUEST_LOAD)
{
String PathLoadFile = data.getStringExtra(FileDialog.RESULT_PATH);
//here we have the path from the file in the string and we can read from it.
}
}

 

Helpful read/write/connect code

 

All credits for the making and sharing the file browser go to Alexander! His Google code site:

http://android-file-dialog.googlecode.com

Comments
  1. shiva says:

    Hi… Wonderful tutorial.. I was wandering everywhere for this. One Humble request. Can you please make another tutorial of doing it in Android Studio?? (Windows) pleeeeez….

    Thanks..!!

    • degoossez says:

      Sorry for the late reply, I’m busy with my finals.
      I don’t think it can be that hard to import the package?
      I’ll look into it when I find the time. That will be somewhere at the end of February

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s