Create File Manager android code


Tutorial shows you to create a Android Layout showing the file of particular directory or folder.

First,

Create a Layout which where we will show the list of files.
For this create LinearLayout with two TextView.
Name the file as "fileview.xml"




Step 1:


<?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>




Step 2:

Now, create a class "Option.java"


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();
}
}

Step 3:

Create another class "FileArrayAdapter.java"



import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
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;
       }

}


Step 4:


Create class "FileChooser.java"


import java.io.File;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class FileChooser extends ListActivity {


    private File currentDir;
    private FileArrayAdapter adapter;
    final int CONTEXT_MENU_DELETE_ITEM =1;
    final int CONTEXT_MENU_UPDATE =2;
    @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"))
 adapter = new FileArrayAdapter(FileChooser.this,R.layout.fileview,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();
    }
    private void finishscreen() {
        this.finish();
    }

}




Step 5:
In AndroidManifest,
add the permission:

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

Comments