Advertisement
Guest User

Untitled

a guest
Jun 16th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. package ant.h2h.feelsafe;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FilenameFilter;
  7. import java.io.InputStream;
  8. import java.util.ArrayList;
  9.  
  10. import android.app.Activity;
  11. import android.app.AlertDialog;
  12. import android.app.Dialog;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapFactory;
  18. import android.graphics.drawable.BitmapDrawable;
  19. import android.graphics.drawable.Drawable;
  20. import android.os.Bundle;
  21. import android.os.Environment;
  22. import android.util.Log;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.ArrayAdapter;
  26. import android.widget.ListAdapter;
  27. import android.widget.TextView;
  28. import android.widget.Toast;
  29.  
  30. public class FileExploreActivity extends Activity {
  31.  
  32.     // Stores names of traversed directories
  33.     ArrayList<String> str = new ArrayList<String>();
  34.  
  35.     // Check if the first level of the directory structure is the one showing
  36.     private Boolean firstLvl = true;
  37.  
  38.     private static final String TAG = "F_PATH";
  39.  
  40.     private Item[] fileList;
  41.     private File path = new File(Environment.getExternalStorageDirectory() + "");
  42.     private String chosenFile;
  43.     private static final int DIALOG_LOAD_FILE = 1000;
  44.  
  45.     ListAdapter adapter;
  46.  
  47.     @Override
  48.     public void onCreate(Bundle savedInstanceState) {
  49.  
  50.         super.onCreate(savedInstanceState);
  51.  
  52.         loadFileList();
  53.  
  54.         showDialog(DIALOG_LOAD_FILE);
  55.         Log.d(TAG, path.getAbsolutePath());
  56.  
  57.     }
  58.  
  59.     private void loadFileList() {
  60.         try {
  61.             path.mkdirs();
  62.         } catch (SecurityException e) {
  63.             Log.e(TAG, "unable to write on the sd card ");
  64.         }
  65.  
  66.         // Checks whether path exists
  67.         if (path.exists()) {
  68.             FilenameFilter filter = new FilenameFilter() {
  69.                 @Override
  70.                 public boolean accept(File dir, String filename) {
  71.                     File sel = new File(dir, filename);
  72.                     // Filters based on whether the file is hidden or not
  73.                     return (sel.isFile() || sel.isDirectory())
  74.                             && !sel.isHidden();
  75.  
  76.                 }
  77.             };
  78.  
  79.             String[] fList = path.list(filter);
  80.             fileList = new Item[fList.length];
  81.             for (int i = 0; i < fList.length; i++) {
  82.                 fileList[i] = new Item(fList[i], getResources().getDrawable(R.drawable.file_icon));
  83.  
  84.                 // Convert into file path
  85.                 File sel = new File(path, fList[i]);
  86.  
  87.                 // Set drawables
  88.                 if (sel.isDirectory()) {
  89.                     fileList[i].icon = getResources().getDrawable(R.drawable.directory_icon);
  90.                     Log.d("DIRECTORY", fileList[i].file);
  91.                 } else {
  92.                     //get file extension
  93.                     String ext = getFileExtension(fileList[i].file);
  94.                     if((ext.equals("jpg")||ext.equals("png"))){
  95.                         String image = path+"/"+fileList[i].file;
  96.                         InputStream is;
  97.                         try {
  98.                             is = new FileInputStream(image);
  99.                             BitmapFactory.Options options=new BitmapFactory.Options();
  100.                             options.inSampleSize = 8;
  101.                             Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
  102.                            
  103.                             fileList[i].icon = new BitmapDrawable(getResources(),preview_bitmap);
  104.                         } catch (FileNotFoundException e) {
  105.                             e.printStackTrace();
  106.                         }
  107.                        
  108.                     }
  109.                     Log.d("FILE", fileList[i].file);
  110.                 }
  111.             }
  112.  
  113.             if (!firstLvl) {
  114.                 Item temp[] = new Item[fileList.length + 1];
  115.                 for (int i = 0; i < fileList.length; i++) {
  116.                     temp[i + 1] = fileList[i];
  117.                 }
  118.                 temp[0] = new Item("Up", getResources().getDrawable(R.drawable.directory_up));
  119.                 fileList = temp;
  120.             }
  121.         } else {
  122.             Log.e(TAG, "path does not exist");
  123.         }
  124.  
  125.         adapter = new ArrayAdapter<Item>(this,
  126.                 android.R.layout.select_dialog_item, android.R.id.text1,
  127.                 fileList) {
  128.             @Override
  129.             public View getView(int position, View convertView, ViewGroup parent) {
  130.                 // creates view
  131.                 View view = super.getView(position, convertView, parent);
  132.                 TextView textView = (TextView) view
  133.                         .findViewById(android.R.id.text1);
  134.  
  135.                 // put the image on the text view
  136.                 textView.setCompoundDrawablesWithIntrinsicBounds(
  137.                         fileList[position].icon, null, null, null);
  138.  
  139.                 // add margin between image and text (support various screen
  140.                 // densities)
  141.                 int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
  142.                 textView.setCompoundDrawablePadding(dp5);
  143.  
  144.                 return view;
  145.             }
  146.         };
  147.  
  148.     }
  149.  
  150.     private class Item {
  151.         public String file;
  152.         public Drawable icon;
  153.  
  154.         public Item(String file, Drawable drawable) {
  155.             this.file = file;
  156.             this.icon = drawable;
  157.         }
  158.  
  159.         @Override
  160.         public String toString() {
  161.             return file;
  162.         }
  163.     }
  164.  
  165.     @Override
  166.     protected Dialog onCreateDialog(int id) {
  167.         Dialog dialog = null;
  168.         final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  169.  
  170.         if (fileList == null) {
  171.             Log.e(TAG, "No files loaded");
  172.             dialog = builder.create();
  173.             return dialog;
  174.         }
  175.  
  176.         switch (id) {
  177.         case DIALOG_LOAD_FILE:
  178.             builder.setTitle("Choose your file");
  179.             builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
  180.                 @Override
  181.                 public void onClick(DialogInterface dialog, int which) {
  182.                     chosenFile = fileList[which].file;
  183.                     File sel = new File(path + "/" + chosenFile);
  184.                     if (sel.isDirectory()) {
  185.                         firstLvl = false;
  186.  
  187.                         // Adds chosen directory to list
  188.                         str.add(chosenFile);
  189.                         fileList = null;
  190.                         path = new File(sel + "");
  191.  
  192.                         loadFileList();
  193.                        
  194.                        
  195.                         //builder.setMessage(DIALOG_LOAD_FILE);
  196.                         removeDialog(DIALOG_LOAD_FILE);
  197.                         showDialog(DIALOG_LOAD_FILE);
  198.                         Log.d(TAG, path.getAbsolutePath());
  199.  
  200.                     }
  201.  
  202.                     // Checks if 'up' was clicked
  203.                     else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {
  204.  
  205.                         // present directory removed from list
  206.                         String s = str.remove(str.size() - 1);
  207.  
  208.                         // path modified to exclude present directory
  209.                         path = new File(path.toString().substring(0,
  210.                                 path.toString().lastIndexOf(s)));
  211.                         fileList = null;
  212.  
  213.                         // if there are no more directories in the list, then
  214.                         // its the first level
  215.                         if (str.isEmpty()) {
  216.                             firstLvl = true;
  217.                         }
  218.                         loadFileList();
  219.  
  220.                         //builder.setMessage(DIALOG_LOAD_FILE);
  221.                         removeDialog(DIALOG_LOAD_FILE);
  222.                         showDialog(DIALOG_LOAD_FILE);
  223.                         Log.d(TAG, path.getAbsolutePath());
  224.  
  225.                     }
  226.                     // File picked
  227.                     else {
  228.                         // Perform action with file picked
  229.                         if(getFileExtension(chosenFile).equals("jpg")||getFileExtension(chosenFile).equals("png")){
  230.                             Log.d("FILE SEND", path+"/"+chosenFile);
  231.                             Intent i=new Intent();
  232.                             i.setAction("file");
  233.                             i.putExtra("file",path+"/"+chosenFile);
  234.                             sendBroadcast(i);
  235.                            
  236.                         }
  237.                         else{
  238.                             Context context = getApplicationContext();
  239.                             CharSequence text = getResources().getString(R.string.follow_register_wrong_img_format);
  240.                             int duration = Toast.LENGTH_SHORT;
  241.  
  242.                             Toast toast = Toast.makeText(context, text, duration);
  243.                             toast.show();
  244.                         }
  245.                         finish();
  246.                     }
  247.  
  248.                 }
  249.             });
  250.             break;
  251.         }
  252.         dialog = builder.show();
  253.         return dialog;
  254.     }
  255.    
  256.     public String getFileExtension(String fileName){
  257.         String extension = "";
  258.  
  259.         int i = fileName.lastIndexOf('.');
  260.         int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
  261.  
  262.         if (i > p) {
  263.             extension = fileName.substring(i+1);
  264.         }
  265.         return extension;
  266.     }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement