This is what the result is goning to look like:

Stating this tutorial assumes that you already have Eclipse and the Android SDK installed on your computer.
Ok well here we go!
To start off we first need to create a new project...
In eclipse do File -> New -> Android Project
• Name the project File Chooser.
• Select the build target 1.6
• For application name put File Chooser
• For package name put whatever you like. I'm going to name my package com.h3r3t1c.filechooser
• Make sure Create Activity is checked and type in FileChooser
• Min SDK Version is 4
Now just select ok.
Before we start coding we need to set the permission so we can read and write file on the SD Card so to do this open the AndroidManifest.xml and click on the permissions tab. Click the add button and select Uses Permission.
In the drop down menu select android.permission.WRITE_EXTERNAL_STORAGE and click save.
Now to start the coding!
Expand src and open the file FileChooser.java
The first thing you'll notice is that Eclipse auto generated some code for us. This is good but we need to change a few things. First thing we need to change is activity. Rename extends Activity to extends ListActivity. Next we need to remove setContentView(R.layout.main);.
You should now have something that looks like this:
public class FileChooser extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Ok the next thing were going to want to do is to start adding our methods.
The first method were going to create is called fill. The purpose of fill is to get all the files and folder for the current directory were in.
In your FileChooser class create the method:
private void fill(File f)
{
}
Now that we have that created were going to want to have it called right away when the activity starts so to do this first we need to create a current directory variable. Above the onCreate() method add this:
private File currentDir;Note: your going to have to import File!
Now going back to the onCreate() method we need to initialize currentDir to the root of the SD Card so to do this add this:
currentDir = new File("/sdcard/)
now under that call the fill method like so:
fill(currentDir);
Right now our class should look like this :
public class FileChooser extends ListActivity {
private File currentDir;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDir = new File("/sdcard/");
fill(currentDir);
}
private void fill(File f)
{
}
}
Before we move onto adding the code to we need to create a class to hold our data were going to get from listing all the file and directories so in eclipse right click on your package and select New -> Class.
Name this class Option and select ok.
Add this code to your Option class:
public class Option implements Comparable<Option>{
private String name;
private String data;
private String path;
public Option(String n,String d,String p)
{
name = n;
data = d;
path = p;
}
public String getName()
{
return name;
}
public String getData()
{
return data;
}
public String getPath()
{
return path;
}
@Override
public int compareTo(Option o) {
if(this.name != null)
return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
else
throw new IllegalArgumentException();
}
}
Ok now that that is done go back to FileChooser.java
The fill method is going to work like this:
• Were going to get an array of all the files and dirs in the current were in.
• Were going to create 2 ListArrays. One for folders and one for files.
• Were going to sort files and dirs into the appropriate ListArray.
• Were going to sort the ListArrays alphabetically and pass to one ListArray.
• Were going to pass this ListArray to our custom ArrayAdapter
So our fill method should look like this:
private void fill(File f)
{
File[]dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<Option>dir = new ArrayList<Option>();
List<Option>fls = new ArrayList<Option>();
try{
for(File ff: dirs)
{
if(ff.isDirectory())
dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
else
{
fls.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
}
}
}catch(Exception e)
{
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if(!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new Option("..","Parent Directory",f.getParent()));
}
Now we need to make our custom ArrayAdapter
Create a new class called FileArrayAdapter.
In FileArrayAdapter have it extend ArrayAdapter<Option>, add constructor and add global variables like so:
public class FileArrayAdapter extends ArrayAdapter<Option>{
private Context c;
private int id;
private List<Option>items;
public FileArrayAdapter(Context context, int textViewResourceId,
List<Option> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
items = objects;
}
}
Before we move on we now need to create a View for us to display our data!
Click New -> Android XML File
For file call it file_view.xml.
Click layout and then finish.
Replace the xml text in the file with this text:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent"> <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textStyle="bold" android:layout_marginTop="5dip" android:layout_marginLeft="5dip"></TextView> <TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip"></TextView> </LinearLayout>
Now go back to FileArrayAdaper.
Were going to do the following:
• Add method to get item from a location in the List.
• Override the getView() method
• Using the getView() method create our custom view from the data in items and use file_view.xml to be the view.
Add this code to your FileArrayAdapter:
public Option getItem(int i)
{
return items.get(i);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
final Option o = items.get(position);
if (o != null) {
TextView t1 = (TextView) v.findViewById(R.id.TextView01);
TextView t2 = (TextView) v.findViewById(R.id.TextView02);
if(t1!=null)
t1.setText(o.getName());
if(t2!=null)
t2.setText(o.getData());
}
return v;
}
Now go back to FileChooser and add the global variable private FileArrayAdapter adapter;
Now back in fill method add the following code to initialize our adapter:
adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir); this.setListAdapter(adapter);
At this point you should be able to test it and it should be listing all the folders and files in the root of your SD Card.
Now we need to handle users clicking on files and folders! To do this were going to override the onListItemClick method.
Add this code to your FileChooser class:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
currentDir = new File(o.getPath());
fill(currentDir);
}
}
At this point we should now be able to move through the directories on the SD Card!
Now for our purposes the clicking of files will just show a toast message saying tha a file has been clicked but it will give you and idea of how to handle the files we click!
Create a method called onFileClick() and adding the following code:
private void onFileClick(Option o)
{
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
}
Then go back up to the onListItemClick() and add this code:
else
{
onFileClick(o);
}
The onFileClick method could be used to return the path of the file if FileChooser was called with startActivityForResult().
This is what your classes should look like in the end:
FileChooser:
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class FileChooser extends ListActivity {
private File currentDir;
private FileArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDir = new File("/sdcard/");
fill(currentDir);
}
private void fill(File f)
{
File[]dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<Option>dir = new ArrayList<Option>();
List<Option>fls = new ArrayList<Option>();
try{
for(File ff: dirs)
{
if(ff.isDirectory())
dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
else
{
fls.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
}
}
}catch(Exception e)
{
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if(!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new Option("..","Parent Directory",f.getParent()));
adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir);
this.setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
currentDir = new File(o.getPath());
fill(currentDir);
}
else
{
onFileClick(o);
}
}
private void onFileClick(Option o)
{
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
}
}
FileArrayAdapter:
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class FileArrayAdapter extends ArrayAdapter<Option>{
private Context c;
private int id;
private List<Option>items;
public FileArrayAdapter(Context context, int textViewResourceId,
List<Option> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
items = objects;
}
public Option getItem(int i)
{
return items.get(i);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
final Option o = items.get(position);
if (o != null) {
TextView t1 = (TextView) v.findViewById(R.id.TextView01);
TextView t2 = (TextView) v.findViewById(R.id.TextView02);
if(t1!=null)
t1.setText(o.getName());
if(t2!=null)
t2.setText(o.getData());
}
return v;
}
}
Option:
public class Option implements Comparable<Option>{
private String name;
private String data;
private String path;
public Option(String n,String d,String p)
{
name = n;
data = d;
path = p;
}
public String getName()
{
return name;
}
public String getData()
{
return data;
}
public String getPath()
{
return path;
}
@Override
public int compareTo(Option o) {
if(this.name != null)
return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
else
throw new IllegalArgumentException();
}
}
Next tutorial I will be showing how to add icons for different file types and folders
The full project can be downloaded here:
http://www.sendspace.com/pro/dl/vre5ac





MultiQuote






|