获取图片所在文件夹

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得到上面图的效果了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值