如何将所有文件路径加载到字符串数组中

时间:2013-09-12 23:24:52

标签: java android

我正在尝试将文件路径加载到字符串数组(路径)中,但我被卡住了。这是我的代码:

public class MainActivity extends ListActivity{

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video/";

String[] videoFileList = {

        path,

};

public class MyThumbnaildapter extends ArrayAdapter<String>{

    public MyThumbnaildapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View row = convertView;
        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.row, parent, false);
        }

        ImageView imageThumbnail = (ImageView)row.findViewById(R.id.Thumbnail);

        Bitmap bmThumbnail;
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(videoFileList[position], Thumbnails.MINI_KIND);
        imageThumbnail.setImageBitmap(bmThumbnail);

        return row;
    }

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new MyThumbnaildapter(MainActivity.this, R.layout.row, videoFileList));
}

}

我正在尝试加载视频路径。我可以加载一个特定文件,但不能加载目录中的所有文件。任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

看看File.list()。使用文件路径创建File对象。 list()将返回String的数组。

请求的示例。

File pathToFiles = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video/");

String[] videoFileList = pathToFiles.list();

答案 1 :(得分:0)

要迭代给定目录的文件,请使用

new File(path).listFiles()

或某些新的Java 7 API,例如:

Path dir = new File(path).toPath();     
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir);