[android] 모든 이미지 파일 목록 가져오기

 

private ArrayList<String> getPathOfAllImages()
{
  ArrayList<String> result = new ArrayList<>();
  Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  String[] projection = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
  
  Cursor cursor = getContentResolver().query(uri, projection, null, null, MediaColumns.DATE_ADDED + " desc");
  int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
  int columnDisplayname = cursor.getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME);
  
  int lastIndex;
  while (cursor.moveToNext())
  {
    String absolutePathOfImage = cursor.getString(columnIndex);
    String nameOfFile = cursor.getString(columnDisplayname);
    lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
    lastIndex = lastIndex >= 0 ? lastIndex : nameOfFile.length() - 1;
    
    if (!TextUtils.isEmpty(absolutePathOfImage))
    {
      result.add(absolutePathOfImage);
    }
  }
  
  for (String string : result)
  {
    Log.i("PhotoSelectActivity.java | getPathOfAllImages", "|" + string + "|");
  }
  return result;
}

 

/storage/sdcard0/DCIM/100LGDSC/CAM00082.jpg
/storage/sdcard0/DCIM/100LGDSC/CAM00083.jpg
/storage/sdcard0/DCIM/100LGDSC/CAM00084.jpg
/storage/sdcard0/DCIM/100LGDSC/CAM00085.jpg
/storage/sdcard0/DCIM/asdf/screenshot_2015-03-05_PM 2.22.43.png
/storage/sdcard0/DCIM/asdf/screenshot_2015-03-05_PM 2.22.45.png
/storage/sdcard0/DCIM/asdf/screenshot_2015-03-05_PM 2.22.47.png
/storage/sdcard0/Pictures/IMG_20150305_080307_309921097.jpg
/storage/sdcard0/DCIM/100LGDSC/CAM00086.jpg