document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.kant.MangoPlayer;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5. import java.util.ArrayList;
  6.  
  7. import com.kant.MangoPlayer.R;
  8. import android.app.Activity;
  9. import android.app.AlertDialog;
  10. import android.app.Dialog;
  11. import android.app.AlertDialog.Builder;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.os.Bundle;
  15. import android.os.Environment;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.ArrayAdapter;
  20. import android.widget.ListAdapter;
  21. import android.widget.TextView;
  22.  
  23. /**
  24.  * @author kantsh
  25.  *      This code can be used for development purposes and is free to use.
  26.  *         Completed on Tuesday,April 24,2012
  27.  *         mailto "kant.shashi@hotmail.com"
  28.  */
  29.  
  30. public class FileExplore extends Activity {
  31.  ArrayList<string> str = new ArrayList<string>();
  32.  
  33.  // Check if the first level of the directory structure is the one showing
  34.  private Boolean firstLvl = true;
  35.  
  36.  private static final String TAG = "F_PATH";
  37.  
  38.  private Item[] fileList;
  39.  private File path = new File(Environment.getExternalStorageDirectory() + "");
  40.  private String chosenFile;
  41.  private static final int DIALOG_LOAD_FILE = 1000;
  42.  
  43.  ListAdapter adapter;
  44.  
  45.  @Override
  46.  protected void onCreate(Bundle savedInstanceState) {
  47.   // TODO Auto-generated method stub
  48.   super.onCreate(savedInstanceState);
  49.   setContentView(R.layout.file_explorer);
  50.  
  51.   loadFileList();
  52.  
  53.   showDialog(DIALOG_LOAD_FILE);
  54.   Log.d(TAG, path.getAbsolutePath());
  55.  
  56.  }
  57.  
  58.  private void loadFileList() {
  59.   try {
  60.    path.mkdirs();
  61.   } catch (SecurityException e) {
  62.    Log.e(TAG, "unable to write on the sd card ");
  63.   }
  64.  
  65.   // Checks whether path exists
  66.   if (path.exists()) {
  67.    // create a filter that helps to look for files directories and
  68.    // hidden files
  69.    FilenameFilter filter = new FilenameFilter() {
  70.     @Override
  71.     public boolean accept(File dir, String filename) {
  72.      File sel = new File(dir, filename);
  73.      // Filters based on whether the file is hidden or not
  74.      return (sel.isFile() || sel.isDirectory())
  75.        &amp;&amp; !sel.isHidden();
  76.     }
  77.    };
  78.  
  79.    String[] fList = path.list(filter);
  80.    fileList = new Item[fList.length];
  81.    for (int i = 0; i &lt; fList.length; i++) {
  82.     fileList[i] = new Item(fList[i], R.drawable.file_icon); // by
  83.                   // default
  84.                   // icon
  85.  
  86.     // Convert into file path
  87.     File sel = new File(path, fList[i]);
  88.  
  89.     // Set drawables
  90.     if (sel.isDirectory()) {
  91.      fileList[i].icon = R.drawable.directory_icon;
  92.      Log.d("DIRECTORY", fileList[i].file);
  93.     }
  94.  
  95.     // pdf format
  96.     else if (sel.getName().endsWith(".pdf")) {
  97.      fileList[i].icon = R.drawable.pdf_file;
  98.      Log.d("FILE", fileList[i].file);
  99.     }
  100.  
  101.     // image formats
  102.     else if (sel.getName().endsWith(".jpeg")
  103.       || sel.getName().endsWith(".jpg")) {
  104.      fileList[i].icon = R.drawable.jpg_file;
  105.      Log.d("FILE", fileList[i].file);
  106.     } else if (sel.getName().endsWith(".gif")) {
  107.      fileList[i].icon = R.drawable.gif;
  108.      Log.d("FILE", fileList[i].file);
  109.     }
  110.  
  111.     // audio and some common formats
  112.     else if (sel.getName().endsWith(".mp3")
  113.       || sel.getName().endsWith(".MP3")) {
  114.      fileList[i].icon = R.drawable.mp3_file;
  115.      Log.d("FILE", fileList[i].file);
  116.     } else if (sel.getName().endsWith(".mpg")
  117.       || sel.getName().endsWith(".mpeg")) {
  118.      fileList[i].icon = R.drawable.mpeg_file;
  119.      Log.d("FILE", fileList[i].file);
  120.     } else if (sel.getName().endsWith(".avi")) {
  121.      fileList[i].icon = R.drawable.avi_file;
  122.      Log.d("FILE", fileList[i].file);
  123.     }
  124.     // all other formats for video
  125.     else if (sel.getName().endsWith(".mov")
  126.       || sel.getName().endsWith(".asf")
  127.       || sel.getName().endsWith(".mp4")
  128.       || sel.getName().endsWith(".3gp")
  129.       || sel.getName().endsWith(".flv")
  130.       || sel.getName().endsWith(".rm")
  131.       || sel.getName().endsWith(".wmv")
  132.       || sel.getName().endsWith(".mkv")) {
  133.      fileList[i].icon = R.drawable.video_icon;
  134.      Log.d("FILE", fileList[i].file);
  135.     }
  136.  
  137.     // archives file formats
  138.     else if (sel.getName().endsWith(".zip")
  139.       || sel.getName().endsWith(".rar")
  140.       || sel.getName().endsWith(".jar")
  141.       || sel.getName().endsWith(".gzip")) {
  142.      fileList[i].icon = R.drawable.zip_icon;
  143.      Log.d("FILE", fileList[i].file);
  144.     }
  145.  
  146.     else {
  147.      Log.d("FILE", fileList[i].file);
  148.     }
  149.    }
  150.  
  151.    // show the up symbol only when u r not at the top of the path
  152.    if (!firstLvl) {
  153.     Item temp[] = new Item[fileList.length + 1];
  154.     for (int i = 0; i &lt; fileList.length; i++) {
  155.      temp[i + 1] = fileList[i];
  156.     }
  157.     temp[0] = new Item("Up", R.drawable.directory_up);
  158.     fileList = temp;
  159.    }
  160.   } else {
  161.    Log.e(TAG, "path does not exist");
  162.   }
  163.  
  164.   adapter = new ArrayAdapter<item>(this,
  165.     android.R.layout.select_dialog_item, android.R.id.text1,
  166.     fileList) {
  167.    @Override
  168.    public View getView(int position, View convertView, ViewGroup parent) {
  169.     // creates view
  170.     View view = super.getView(position, convertView, parent);
  171.     TextView textView = (TextView) view
  172.       .findViewById(android.R.id.text1);
  173.  
  174.     // put the image on the text view
  175.     textView.setCompoundDrawablesWithIntrinsicBounds(
  176.       fileList[position].icon, 0, 0, 0);
  177.  
  178.     // add margin between image and text (support various screen
  179.     // densities)
  180.     int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
  181.     textView.setCompoundDrawablePadding(dp5);
  182.  
  183.     return view;
  184.    }
  185.   };
  186.  
  187.  }
  188.  
  189.  private class Item {
  190.   public String file;
  191.   public int icon;
  192.  
  193.   public Item(String file, Integer icon) {
  194.    this.file = file;
  195.    this.icon = icon;
  196.   }
  197.  
  198.   @Override
  199.   public String toString() {
  200.    return file;
  201.   }
  202.  }
  203.  
  204.  @Override
  205.  protected Dialog onCreateDialog(int id) {
  206.   Dialog dialog = null;
  207.   AlertDialog.Builder builder = new Builder(this);
  208.  
  209.   if (fileList == null) {
  210.    Log.e(TAG, "No files loaded");
  211.    dialog = builder.create();
  212.    return dialog;
  213.   }
  214.  
  215.   switch (id) {
  216.   case DIALOG_LOAD_FILE:
  217.    builder.setTitle("Browse your file");
  218.    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
  219.     @Override
  220.     public void onClick(DialogInterface dialog, int which) {
  221.      chosenFile = fileList[which].file;
  222.      File sel = new File(path + "/" + chosenFile);
  223.      if (sel.isDirectory()) {
  224.       firstLvl = false;
  225.  
  226.       // Adds chosen directory to list
  227.       str.add(chosenFile);
  228.       fileList = null;
  229.       path = new File(sel + "");
  230.  
  231.       loadFileList();
  232.       // refresh the list being shown
  233.       removeDialog(DIALOG_LOAD_FILE);
  234.       showDialog(DIALOG_LOAD_FILE);
  235.  
  236.       Log.d(TAG, path.getAbsolutePath());
  237.      }
  238.  
  239.      // Checks if \'up\' was clicked
  240.      else if (chosenFile.equalsIgnoreCase("up") &amp;&amp; !sel.exists()) {
  241.  
  242.       // present directory removed from list
  243.       String s = str.remove(str.size() - 1);
  244.  
  245.       // path modified to exclude present directory
  246.       path = new File(path.toString().substring(0,
  247.         path.toString().lastIndexOf(s)));
  248.       fileList = null;
  249.  
  250.       // if there are no more directories in the list, then
  251.       // its the first level
  252.       if (str.isEmpty()) {
  253.        firstLvl = true;
  254.       }
  255.       loadFileList();
  256.  
  257.       removeDialog(DIALOG_LOAD_FILE);
  258.       showDialog(DIALOG_LOAD_FILE);
  259.       Log.d(TAG, path.getAbsolutePath());
  260.  
  261.      }
  262.      // File picked.......supposed to return fileName chosen by
  263.      // the User
  264.      else {
  265.       // Perform action with file picked
  266.       Intent intent = getIntent();
  267.       String returnVal = path.getPath() + "/" + chosenFile;
  268.       intent.putExtra("fileChosen", returnVal);
  269.       setResult(RESULT_OK, intent);
  270.       // for purpose of testing
  271.       // Toast.makeText(getBaseContext(),
  272.       // returnVal,Toast.LENGTH_LONG).show();
  273.       finish();
  274.      }
  275.  
  276.     }
  277.    });
  278.    break;
  279.   }
  280.   dialog = builder.show();
  281.   return dialog;
  282.  }
  283. }
');