Home > Uncategorized > Directory/File Browser Example Application

Directory/File Browser Example Application

Here’s a sample that allows you to browse the file system on your Android. I found some sample code at http://www.linuxdevices.com/articles/AT6247038002.html and adapted it slightly.

Note that it extends ListActivity and the items in the list are are rendered using the layout file file_list_row.xml.

Here’s the code…

Here’s the main layout file (main.xml)…

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
<ListView android:id=”@android:id/list”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
<TextView android:id=”@android:id/empty”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/empty_dir”/>
</LinearLayout>

Here’s the layout file for the items in the ListActivity (file_list_row.xml)…

<?xml version=”1.0″ encoding=”utf-8″?>
<TextView android:id=”@+id/text1″
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>

Here’s the main activity…

package com.sst.directory.file.handling;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DirectoryBrowser extends ListActivity {

private List items = null;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
getFiles(new File(“/”).listFiles());
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
int selectedRow = (int)id;
if(selectedRow == 0){
getFiles(new File(“/”).listFiles());
}else{
File file = new File(items.get(selectedRow));
if(file.isDirectory()){
getFiles(file.listFiles());
}else{
new AlertDialog.Builder(DirectoryBrowser.this)
.setTitle(“This file is not a directory”)
.setNeutralButton(“OK”, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int button){
//do nothing
}
})
.show();
}
}
}
private void getFiles(File[] files){
items = new ArrayList();
items.add(getString(R.string.goto_root));
for(File file : files){
items.add(file.getPath());
}
ArrayAdapter fileList = new ArrayAdapter(this,R.layout.file_list_row, items);
setListAdapter(fileList);
}
}

In string.xml file, you need to add string :-
<string name=”goto_root”>Goto Root</string>
<string name=”empty_dir”>Empty Dir</string>

Full Source code is attached here as .zip, you can download and use it :-

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: