Advertisement
marquessbr

Listar arquivos no android

Mar 30th, 2020
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. //package com.seupackage.seudominio.seuapp;
  2. // precisa resolver isso com o seu package
  3. /**
  4.  * Created by armando on 17/04/17.
  5.  */
  6.  
  7. import android.app.ListActivity;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.content.res.Configuration;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.AdapterView;
  17. import android.widget.AdapterView.OnItemClickListener;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.ListAdapter;
  20. import android.widget.TextView;
  21.  
  22. //import com.seupackage.seudominio.seuapp.R;
  23. // precisa resolver isso com o seu package
  24.  
  25. import java.io.File;
  26. import java.io.FilenameFilter;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.Comparator;
  30. import java.util.Locale;
  31.  
  32. public class FileExploreActivity extends ListActivity {
  33.  
  34.     // Stores names of traversed directories
  35.     ArrayList<String> str = new ArrayList<String>();
  36.  
  37.     // Check if the first level of the directory structure is the one showing
  38.     private Boolean firstLvl = true;
  39.  
  40.     private Item[] fileList;
  41.     private File path;
  42.  
  43.     private String chosenFile;
  44.     private ListAdapter adapter;
  45.  
  46.     private String local;
  47.     private String region;
  48.  
  49.     private SharedPreferences ep;
  50.  
  51.     @Override
  52.     public void onCreate(Bundle savedInstanceState) {
  53.         String data = getIntent().getExtras().getString("path");
  54.         path = new File(data);
  55.  
  56.         super.onCreate(savedInstanceState);
  57.         ep = getSharedPreferences("Settings", 0);
  58.  
  59.         // Enforce internal language change
  60.         local = ep.getString("language", "en");
  61.         region = ep.getString("region", "");
  62.         Locale locale2 = new Locale(local, region);
  63.         Locale.setDefault(locale2);
  64.         Configuration config2 = new Configuration();
  65.         config2.locale = locale2;
  66.         getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources()
  67.                 .getDisplayMetrics());
  68.  
  69.         loadFileList();
  70.         getListView().setAdapter(adapter);
  71.  
  72.         getListView().setOnItemClickListener(new OnItemClickListener() {
  73.  
  74.             public void onItemClick(AdapterView<?> arg0, View arg1, int which, long arg3) {
  75.                 chosenFile = fileList[which].file;
  76.  
  77.                 File sel = new File(path + "/" + chosenFile);
  78.  
  79.                 if (sel.isDirectory()) {
  80.                     firstLvl = false;
  81.  
  82.                     // Adds chosen directory to list
  83.                     str.add(chosenFile);
  84.                     fileList = null;
  85.                     path = new File(sel + "");
  86.  
  87.                     loadFileList();
  88.  
  89.                     getListView().setAdapter(adapter);
  90.                 }
  91.  
  92.                 // Checks if 'up' was clicked
  93.                 else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {
  94.  
  95.                     // present directory removed from list
  96.                     String s = str.remove(str.size() - 1);
  97.  
  98.                     // path modified to exclude present directory
  99.                     path = new File(path.toString().substring(0,
  100.                             path.toString().lastIndexOf(s)));
  101.                     fileList = null;
  102.  
  103.                     // if there are no more directories in the list, then
  104.                     // its the first level
  105.                     if (str.isEmpty()) {
  106.                         firstLvl = true;
  107.                     }
  108.                     loadFileList();
  109.                     getListView().setAdapter(adapter);
  110.                 }
  111.                 // File picked
  112.                 else {
  113.                     try {
  114.                         Intent returnIntent = new Intent();
  115.                         returnIntent.putExtra("result", sel.toString());
  116.                         setResult(RESULT_OK, returnIntent);
  117.                         finish();
  118.                     } catch (Exception e) {
  119.                         e.printStackTrace();
  120.                     }
  121.                 }
  122.             }
  123.         });
  124.     }
  125.  
  126.     private void loadFileList() {
  127.         try {
  128.             path.mkdirs();
  129.         } catch (SecurityException e) {
  130.         }
  131.  
  132.         // Checks whether path exists
  133.         if (path.exists()) {
  134.  
  135.             File[] filesList = path.listFiles(new FilenameFilter() {
  136.                 public boolean accept(File dir, String filename) { //TODO add option to exclude certain filenames from search
  137.                     File sel = new File(dir, filename);
  138.                     if (sel.toString().contains(".fieldbook")||sel.toString().contains("severity.txt")||sel.toString().contains("sharedpref.xml")){
  139.                         return false;
  140.                     }
  141.                     return true;
  142.                 }
  143.             });
  144.  
  145.             Arrays.sort(filesList, comp);
  146.             String[] fList = new String[filesList.length];
  147.  
  148.             for (int i = 0; i < filesList.length; ++i) {
  149.                 fList[i] = filesList[i].getName();
  150.             }
  151.  
  152.             fileList = new Item[fList.length];
  153.  
  154.             for (int i = 0; i < fList.length; i++) {
  155.                 fileList[i] = new Item(fList[i], R.drawable.file_icon);
  156.  
  157.                 // Convert into file path
  158.                 File sel = new File(path, fList[i]);
  159.  
  160.                 // Set drawables
  161.                 if (sel.isDirectory()) {
  162.                     fileList[i].icon = R.drawable.directory_icon;
  163.                     Log.d("DIRECTORY", fileList[i].file);
  164.                 }
  165.                 if (sel.toString().toLowerCase().contains(".bmp")) {
  166.                     fileList[i].icon = R.drawable.bmp;
  167.                 }
  168.  
  169.                 if (sel.toString().toLowerCase().contains(".jpg")) {
  170.                     fileList[i].icon = R.drawable.bmp;
  171.                 }
  172.  
  173.                 if (sel.toString().toLowerCase().contains(".png")) {
  174.                     fileList[i].icon = R.drawable.bmp;
  175.                 } else {
  176.                     Log.d("FILE", fileList[i].file);
  177.                 }
  178.             }
  179.  
  180.             if (!firstLvl) {
  181.                 Item temp[] = new Item[fileList.length + 1];
  182.                 for (int i = 0; i < fileList.length; i++) {
  183.                     temp[i + 1] = fileList[i];
  184.                 }
  185.                 temp[0] = new Item("Up", R.drawable.directory_up);
  186.                 fileList = temp;
  187.             }
  188.         }
  189.  
  190.         adapter = new ArrayAdapter<Item>(this,
  191.                 android.R.layout.select_dialog_item, android.R.id.text1,
  192.                 fileList) {
  193.             @Override
  194.             public View getView(int position, View convertView, ViewGroup parent) {
  195.                 // creates view
  196.                 View view = super.getView(position, convertView, parent);
  197.                 TextView textView = (TextView) view
  198.                         .findViewById(android.R.id.text1);
  199.  
  200.                 // put the image on the text view
  201.                 textView.setCompoundDrawablesWithIntrinsicBounds(
  202.                         fileList[position].icon, 0, 0, 0); //TODO fix this for phones (text too big and icons are terrible)
  203.  
  204.                 // add margin between image and text (support various screen
  205.                 // densities)
  206.                 int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
  207.                 textView.setCompoundDrawablePadding(dp5);
  208.  
  209.                 return view;
  210.             }
  211.         };
  212.     }
  213.  
  214.     Comparator comp = new Comparator() {
  215.         public int compare(Object o1, Object o2) {
  216.             File f1 = (File) o1;
  217.             File f2 = (File) o2;
  218.             if (f1.isDirectory() && !f2.isDirectory()) {
  219.                 // Directory before non-directory
  220.                 return -1;
  221.             } else if (!f1.isDirectory() && f2.isDirectory()) {
  222.                 // Non-directory after directory
  223.                 return 1;
  224.             } else {
  225.                 // Alphabetic order otherwise
  226.                 return f1.toString().compareToIgnoreCase(f2.toString());
  227.             }
  228.         }
  229.     };
  230.  
  231.     // Wrapper class to hold file data
  232.     private class Item {
  233.         public String file;
  234.         public int icon;
  235.  
  236.         public Item(String file, Integer icon) {
  237.             this.file = file;
  238.             this.icon = icon;
  239.         }
  240.  
  241.         @Override
  242.         public String toString() {
  243.             return file;
  244.         }
  245.     }
  246.  
  247.     @Override
  248.     public boolean onOptionsItemSelected(MenuItem item) {
  249.  
  250.         switch (item.getItemId()) {
  251.             case android.R.id.home:
  252.                 finish();
  253.                 break;
  254.         }
  255.  
  256.         return super.onOptionsItemSelected(item);
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement