Friday, September 7, 2012

File Explorer with sorting, in alphabetical and lastModified.

I have a former exercise of "File Explorer", with Sorting in alphabetical order ignore case, and Sorting in lastModified time. Here, I will group them together to make buttons for user to select Sorting method.

File Explorer with sorting


Modify layout file to add buttons for sorting method.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button_alphabetically"
            android:layout_width="0dp" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="alphabetically"/>
        <Button
            android:id="@+id/button_lastDateModified"
            android:layout_width="0dp" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="lastDateModified"/>
    </LinearLayout>
    <TextView
        android:id="@+id/path"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="No Data"
        />

</LinearLayout>


Main code:
package com.example.androidexplorer;

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

import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
 
 private List<String> item = null;
 private List<String> path = null;
 private String root;
 private TextView myPath;
 
 private String currentPath;
 Comparator<? super File> comparator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myPath = (TextView)findViewById(R.id.path);
        
        comparator = filecomparatorByAlphabetically;
        root = Environment.getExternalStorageDirectory().getPath();
        getDir(root);
        
        Button btnAlphabetically = (Button)findViewById(R.id.button_alphabetically);
        btnAlphabetically.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    comparator = filecomparatorByAlphabetically;
    getDir(currentPath);
    
   }});
        
        Button btnLastDateModified = (Button)findViewById(R.id.button_lastDateModified);
        btnLastDateModified.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    comparator = filecomparatorByLastModified;
          getDir(currentPath);
    
   }});
    }
    
    private void getDir(String dirPath)
    {
     currentPath = dirPath;
     
     myPath.setText("Location: " + dirPath);
     item = new ArrayList<String>();
     path = new ArrayList<String>();
     File f = new File(dirPath);
     File[] files = f.listFiles();
     
     if(!dirPath.equals(root))
     {
      item.add(root);
      path.add(root);
      item.add("../");
      path.add(f.getParent()); 
     }
     
     Arrays.sort(files, comparator);
     
     for(int i=0; i < files.length; i++)
     {
      File file = files[i];
      
      if(!file.isHidden() && file.canRead()){
       path.add(file.getPath());
          if(file.isDirectory()){
           item.add(file.getName() + "/");
          }else{
           item.add(file.getName());
          }
      } 
     }

     ArrayAdapter<String> fileList =
       new ArrayAdapter<String>(this, R.layout.row, item);
     setListAdapter(fileList); 
    }
    
    Comparator<? super File> filecomparatorByLastModified = new Comparator<File>(){
  
  public int compare(File file1, File file2) {

   if(file1.isDirectory()){
    if (file2.isDirectory()){
     return Long.valueOf(file1.lastModified()).compareTo(file2.lastModified());
    }else{
     return -1;
    }
   }else {
    if (file2.isDirectory()){
     return 1;
    }else{
     return Long.valueOf(file1.lastModified()).compareTo(file2.lastModified());
    }
   }
    
  }  
 };
    
    Comparator<? super File> filecomparatorByAlphabetically = new Comparator<File>(){
  
  public int compare(File file1, File file2) {

   if(file1.isDirectory()){
    if (file2.isDirectory()){
     return String.valueOf(file1.getName().toLowerCase()).compareTo(file2.getName().toLowerCase());
    }else{
     return -1;
    }
   }else {
    if (file2.isDirectory()){
     return 1;
    }else{
     return String.valueOf(file1.getName().toLowerCase()).compareTo(file2.getName().toLowerCase());
    }
   }
    
  }  
 };
 
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  File file = new File(path.get(position));
  
  if (file.isDirectory())
  {
   if(file.canRead()){
    getDir(path.get(position));
   }else{
    new AlertDialog.Builder(this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("[" + file.getName() + "] folder can't be read!")
     .setPositiveButton("OK", null).show(); 
   } 
  }else {
   new AlertDialog.Builder(this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("[" + file.getName() + "]")
     .setPositiveButton("OK", null).show();

    }
 }

}


/res/layout/row.xml, the layout of the rows in the list.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowtext"
    android:layout_width="fill_parent"
    android:layout_height="30sp"
    android:textSize="25sp" />


Please notice that if you target for minimum SDK of level 16, Android 4.1, you have to add permission of READ_EXTERNAL_STORAGE in AndroidManifest.xml.

download filesDownload the files.

Related:
- File Explorer, access SD Card on Samsung Galaxy S3


3 comments:

  1. It doesn't work by my.

    ReplyDelete
  2. I had a problem when the list of files was empty, since f.listFiles();
    returns null if there are no file so I added right after this


    if(files == null){
    files = new File[0];
    }

    ReplyDelete
  3. selected list item index is not correct.

    ReplyDelete