如何在SD卡中列出数据

时间:2011-09-06 08:05:57

标签: android

我想显示位于sdcard中特定位置的文件名。是否可以以listview格式显示它?请指导我。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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(...)

如果您只想获取和显示特定类型的文件(例如mp3文件),也可以使用FilenameFilter。

答案 2 :(得分: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);
        }
    }
}

}