public void getPicParentPath(){
List<String> tempPath = new ArrayList<String>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String projection[] = new String[]{ MediaStore.Images.Media.DATA };
Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null);
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
String filepath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
File file = new File(filepath);
String parentPath = file.getParentFile().getAbsolutePath();
tempPath.add(parentPath);
}
HashSet<String> setPath = new HashSet<String>(tempPath);
List<String> parentFilePath = new ArrayList<String>(setPath);
}
getPicParentPath()是获取手机图库图片文件夹的方法,我们通过cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))这个方法获取所有图片的路径信息,再通过 file.getParentFile().getAbsolutePath()获得图片的父目录文件夹名称,然后把得到的文件夹名称放入一个List中。
但是,此时问题出现了,你会发现,通过上面的方法得到的List会包含大量的相同名称,因为有些图片的父目录是一样的,你没把相同目录名称给去除啊,这时候怎么办呢?
嘿,此时的HashSet就能帮上大忙了,只要把上面的List对象作为参数,直接new一个新的HashSet对象,得到的就是去除了重复元素的数据了。然后再将HashSet对象作为参数,new 一个新的List就得到了我们想要得到的效果了。
然后我们就可以通过listView.setAdapter得到上面图的效果了。