开发者

How to list the data in sdcard

I want to display the file name which is residing in specific location in sdcard. is it possible to开发者_JAVA技巧 display it in listview format? pls guide me.


This is the stuff,

 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);

For more detail look at: Implement a simple File Explorer in Android .


If you know the location/path of the directory from where you want to get the files from, then you can use the following code to get the list of files and display them in list view.

//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);
}

If you are using ListActivity, you wouldn't need to use setContentView(...).

You can also use a FilenameFilter, if you want only specific type of files(e.g mp3 files) to be fetched and displayed.


Use this code,

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);
        }
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜