Android 如何在SD卡中列出数据

Android 如何在SD卡中列出数据,android,Android,我想显示驻留在SD卡特定位置的文件名。是否可以以listview格式显示?请引导我 如果您知道要从中获取文件的目录的位置/路径,则可以使用以下代码获取文件列表并在列表视图中显示它们 //Assuming that MEDIA_PATH is the location from where the files are to be retrieved. List<String> songs=new ArrayList<String>(); File home = new Fi

我想显示驻留在SD卡特定位置的文件名。是否可以以listview格式显示?请引导我

如果您知道要从中获取文件的目录的位置/路径,则可以使用以下代码获取文件列表并在列表视图中显示它们

//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);
}
//假设媒体路径是要从中检索文件的位置。
列表歌曲=新建ArrayList();
文件主目录=新文件(媒体路径);//获取目录的文件表示形式
文件[]musicFiles=home.listFiles();
如果(音乐文件长度>0){
对于(文件f:musicFiles){songs.add(f.getName());}
ListAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,歌曲);
setListAdapter(适配器);
}
如果您使用的是
ListActivity
,则不需要使用
setContentView(…)

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

这就是问题所在

 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);
item=newarraylist();
路径=新的ArrayList();
文件f=新文件(“/sdcard/”);
//或者您可以使用文件f=新文件(Environment.getExternalStorageDirectory().getPAth());
File[]files=f.listFiles();
对于(int i=0;i
有关更多详细信息,请参阅:。

使用此代码

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);
        }
    }
}
公共类录制的视频扩展了ListActivity{
私有列表项=null;
私有列表路径=null;
私有字符串root=“/sdcard/Recorded Videos”;
私有文本视图myPath;
按钮返回;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filelist);
后退=(按钮)findViewById(R.id.back);
ListView lv=getListView();
lv.setTextFilterEnabled(真);
lv.setOnItemLongClickListener(新的OnItemLongClickListener(){
长单击(AdapterView arg0、视图arg1、int arg2、,
长arg3){
File File=新文件(path.get(arg2));
布尔b=file.delete();
返回false;
}
});
myPath=(TextView)findViewById(R.id.path);
getDir(根);
back.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
完成();
}
});
}
私有void getDir(字符串dirPath)
{
setText(“位置:“+dirPath”);
item=newarraylist();
路径=新的ArrayList();
文件f=新文件(dirPath);
File[]files=f.listFiles();
如果(!dirPath.equals(root))
{
项。添加(根);
添加(根目录);
项目.添加(“../”);
add(f.getParent());
}
对于(int i=0;i
}