2014-04-06 60 views
1

我對android比較陌生。目前正在處理一個Gallery應用程序。我的要求是根據文件夾名稱將圖像排列在設備中。我已經設法獲取文件夾中的所有圖像。但我無法根據文件夾名稱進行排列。我面臨的一個問題是,可能有更多的文件夾具有相同的名稱。我將代碼附加在下面。如何根據文件夾名稱在android設備中分組圖像?

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 

回答

相關問題