2011-09-06 50 views
0

数据要显示的是居住在特定的位置在SD卡中的文件名。是否有可能在列表视图格式来显示呢?请指导我。我如何列出SD卡

回答

1

这是东西,

item = new ArrayList<String>(); 
path = new ArrayList<String>(); 

File f = new File("/sdcard/"); 

// or you can use File f=new File(Environment.getExternalStorageDirectory().getPAth()); 
File[] files = f.listFiles(); 


for(int i=0; i < files.length; i++) 
{ 
    File file = files[i]; 
    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); 

更多细节看看:Implement a simple File Explorer in Android

0

如果你知道的目录从那里,你想从文件的位置/路径,那么你可以使用下面的代码来获取文件的列表,并在列表视图中显示它们。

//Assuming that MEDIA_PATH is the location from where the files are to be retrieved. 
List<String> songs=new ArrayList<String>(); 
File home = new File(MEDIA_PATH); // Getting file representation of the directory 
File[] musicFiles=home.listFiles(); 
if(musicFiles.length>0){ 
    for(File f:musicFiles) { songs.add(f.getName()); } 
    ListAdapter adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,songs); 
    setListAdapter(adapter); 
} 

如果您正在使用ListActivity,你就不需要使用setContentView(...)

您还可以使用的FilenameFilter,如果你想只有特定类型的文件(如MP3文件)来读取和显示。

0

使用此代码,

public class RecordedVideos extends ListActivity { 
private List<String> item = null; 
private List<String> path = null; 
private String root="/sdcard/Recorded Videos"; 
private TextView myPath; 
Button back; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.filelist); 
    back=(Button)findViewById(R.id.back); 
    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
    File file = new File(path.get(arg2)); 
     boolean b=file.delete(); 
    return false; 
} 
    }); 

myPath = (TextView)findViewById(R.id.path); 
    getDir(root); 
    back.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
    finish(); 
    } 
}); 
} 


private void getDir(String 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()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
      File file = files[i]; 
      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); 
} 


protected void onListItemClick(ListView l, View v, int position, long id) { 

    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.icon) 
      .setTitle("[" + file.getName() + "] folder can't be read!") 
      .setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 


         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
         } 
        }).show(); 
     } 
    } 
    else 
    { 
     /*new AlertDialog.Builder(this) 
      .setIcon(R.drawable.icon) 
      .setTitle("[" + file.getName() + "]") 
      .setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
         } 
        }).show(); */ 
     Intent intent= new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri filetype= Uri.parse("file://" + file.getPath()); 
     String filename=file.getName(); 
     if(filename.endsWith(".txt")||filename.endsWith(".doc")) 
     { 
     intent.setDataAndType(filetype, "text/*"); 
     startActivity(intent); 
     } 
     else   if(filename.endsWith(".gif")||filename.endsWith(".jpg")||filename.endsWith(".png")) 
     { 
      intent.setDataAndType(filetype, "image/*"); 
      startActivity(intent); 
     } 
     else if(filename.endsWith(".mp4")||filename.endsWith(".3gp")) 
     { 
      intent.setDataAndType(filetype, "video/*"); 
      startActivity(intent); 
     } 
     else 
     { 
      intent.setDataAndType(filetype, "audio/*"); 
      startActivity(intent); 
     } 
    } 
} 

}