显示出sdcard的图片文件

  
  
  
  
  1. package cn.fly.fileResourceManage; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileFilter; 
  5. import java.util.ArrayList; 
  6. import java.util.List; 
  7. import android.app.AlertDialog; 
  8. import android.app.ListActivity; 
  9. import android.content.DialogInterface; 
  10. import android.os.Bundle; 
  11. import android.os.Environment; 
  12. import android.view.View; 
  13. import android.widget.ArrayAdapter; 
  14. import android.widget.ListView; 
  15. import android.widget.TextView; 
  16. import android.widget.Toast; 
  17.  
  18. public class ActivityMain extends ListActivity { 
  19. private TextView lv =null
  20. private List<String> item =null
  21.  
  22. private List<File> paths =null
  23. private File path = null
  24.     @Override 
  25.     public void onCreate(Bundle savedInstanceState) { 
  26.         super.onCreate(savedInstanceState); 
  27.         setContentView(R.layout.main); 
  28.         lv = (TextView)this.findViewById(R.id.tv); 
  29.         boolean sdCardsExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
  30.         if(sdCardsExist){ 
  31.             path=Environment.getExternalStorageDirectory(); 
  32.             file(path); 
  33.         }else
  34.             Toast.makeText(this"SdCard not exist"1).show(); 
  35.         } 
  36.     } 
  37.     private void file(File filePath){ 
  38.         lv.setText(filePath.getPath()); 
  39.         item = new ArrayList<String>(); 
  40.         paths = new ArrayList<File>(); 
  41.  
  42.         File[] files = filePath.listFiles(new filePase()); 
  43.         if(!filePath.equals(path)){ 
  44.             /*第一设置为回到根目录*/ 
  45.             item.add("Back to "+path.getPath()); 
  46.             paths.add(path); 
  47.             /*第二设置为回到上层*/ 
  48.             item.add("Back to ../"); 
  49.             paths.add(filePath.getParentFile()); 
  50.         } 
  51.         for(int i= 0 ;i<files.length ;i++){ 
  52.             File file = files[i]; 
  53.             if(file.canRead()){ 
  54.                 item.add(file.getName()); 
  55.                 paths.add(file.getAbsoluteFile()); 
  56.             } 
  57.         } 
  58.         ArrayAdapter< String> adapter = new ArrayAdapter<String>(this,R.layout.item,R.id.item_tv,item); 
  59.         setListAdapter(adapter); 
  60.     } 
  61.  
  62.     @Override 
  63.     protected void onListItemClick(ListView l, View v, int position, long id) { 
  64.         if(paths.get(position).isDirectory()){ 
  65.             file(paths.get(position)); 
  66.         }else
  67.             new AlertDialog.Builder(this).setIcon(R.drawable.icon).setTitle("["+paths.get(position).getName()+"] is file"
  68.             .setPositiveButton("OK"new DialogInterface.OnClickListener (){ 
  69.                 @Override 
  70.                 public void onClick(DialogInterface dialog, int which) { 
  71.                 } 
  72.                  
  73.             }).show(); 
  74.         } 
  75.     }    
  76.      
  77.     class filePase implements FileFilter{ 
  78.         @Override 
  79.         public boolean accept(File pathname) { 
  80.             String path = pathname.getName(); 
  81.             if(path.endsWith("jpg") || path.endsWith("jpeg")|| path.endsWith("png")|| path.endsWith("gif")|| path.endsWith("bmp")) 
  82.                 return true
  83.             if(pathname.isDirectory()) 
  84.                 return true
  85.             return false
  86.         } 
  87.          
  88.     } 

 

 

你可能感兴趣的:(职场,sdcard,休闲,显示文件,显示sdcard的图片文件)