如何根据文件夹名称在Android设备中分组图像?

时间:2014-04-06 18:08:26

标签: java android gridview gallery

我对android比较新。目前正在处理Gallery应用程序。我的要求是根据文件夹名称在设备中排列图像。我已设法获取文件夹中的所有图像。但我无法根据文件夹名称进行排列。我面临的一个问题是,可能存在多个具有相同名称的文件夹。我正在附加下面的代码。

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media._ID;
            Bitmap thumbnails[] = new Bitmap[3];
            String temp = null;
            int imageCount = 0;

            Cursor imagecursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy);
            int image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);
            int count = imagecursor.getCount();
            String temparrPath = null;
            for (int i = 0; i < count; i++) {
                imagecursor.moveToPosition(i);
                int id = imagecursor.getInt(image_column_index);
                int dataColumnIndex = imagecursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                temparrPath = imagecursor.getString(dataColumnIndex);
                String[] segments = temparrPath.split("/");
                String arrPath = segments[segments.length - 2];
                if (arrPath.equals(temp)) {
                    if (imageCount >= 3){
                        imageCount++;
                        continue;
                    }
                    else {
                        thumbnails[imageCount] = MediaStore.Images.Thumbnails
                                .getThumbnail(getApplicationContext()
                                        .getContentResolver(), id,
                                        MediaStore.Images.Thumbnails.MICRO_KIND,
                                        thumbnailOption);
                        imageCount++;
                    }
                } else {
                    if (i != 0) {
                        Log.d(TAG, "Name = " + temparrPath);
                        adapter.putItem(temp, thumbnails, imageCount);
                        publishProgress();
                    }
                    imageCount = 0;
                    thumbnails[imageCount] = MediaStore.Images.Thumbnails
                            .getThumbnail(getApplicationContext()
                                    .getContentResolver(), id,
                                    MediaStore.Images.Thumbnails.MICRO_KIND,
                                    thumbnailOption);
                    temp = arrPath;
                    imageCount++;
                }
            }
            Log.d(TAG, "Name = " + temparrPath);
            adapter.putItem(temp, thumbnails, imageCount);
            publishProgress();

还有其他办法吗?

这就是我得到的

04-06 23:06:25.080: D/ShowImageFolder(31250): Name = /mnt/sdcard/DCIM/OpenCamera/IMG_20140212_211241.jpg
04-06 23:06:25.110: D/ShowImageFolder(31250): Name = /mnt/sdcard/Download/images.jpg
04-06 23:06:25.150: D/ShowImageFolder(31250): Name = /mnt/sdcard/Edited/IMG_20130810_010215.jpg
04-06 23:06:25.200: D/ShowImageFolder(31250): Name = /mnt/sdcard/noompictures/noompic1382464388594.jpg
04-06 23:06:25.200: D/ShowImageFolder(31250): Name = /mnt/sdcard/DCIM/OpenCamera/IMG_20140315_202138.jpg
04-06 23:06:25.240: D/ShowImageFolder(31250): Name = /mnt/sdcard/DCIM/100ANDRO/DSC_0058.jpg
04-06 23:06:25.280: D/ShowImageFolder(31250): Name = /mnt/sdcard/Pictures/Telegram/IMG_20140320_085838_-1044552010.jpg
04-06 23:06:25.320: D/ShowImageFolder(31250): Name = /mnt/sdcard/DCIM/OpenCamera/IMG_20140330_163630.jpg
04-06 23:06:25.350: D/ShowImageFolder(31250): Name = /mnt/ext_card/WallPaPerS/boat_autumn-wallpaper-1366x768.jpg
04-06 23:06:25.420: D/ShowImageFolder(31250): Name = /mnt/ext_card/DCIM/100ANDRO/DSC_0012.jpg
04-06 23:06:25.480: D/ShowImageFolder(31250): Name = /mnt/ext_card/bluetooth/IMAG0097.jpg
04-06 23:06:25.500: D/ShowImageFolder(31250): Name = /mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20140331-WA0000.jpg
04-06 23:06:25.560: D/ShowImageFolder(31250): Name = /mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20140406-WA0007.jpg

1 个答案:

答案 0 :(得分:0)

将temparrPath放在TreeMap中。密钥的值可以是对适配器的引用。

http://www.mkyong.com/java/how-to-sort-a-map-in-java/

如果您需要的只是名称,那么您可以使用TreeSet并使用比较器打印它:Java PriorityQueue Comparator - How/When do you sort?