title: Retrieve all images directories in android
body: <p>I have tried this to get directories and load images from them but it is very slow to load images and when camera is selected it crashed after some time...</p>
  
  <p>&lt; -------- Code to get directories and images from them ------- ></p>
  
  <pre><code>public static ArrayList&lt;String&gt; getImageBuckets(Context mContext){
      ArrayList&lt;String&gt; buckets = new ArrayList&lt;&gt;();
      Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
      String [] projection = {MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA};
  
      Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
      if(cursor != null){
          File file;
          while (cursor.moveToNext()){
              String bucketPath = cursor.getString(cursor.getColumnIndex(projection[0]));
              String fisrtImage = cursor.getString(cursor.getColumnIndex(projection[1]));
              file = new File(fisrtImage);
              if (file.exists() &amp;&amp; !buckets.contains(bucketPath)) {
                  buckets.add(bucketPath);
              }
          }
          cursor.close();
      }
      return buckets;
  }
  
  
  public static ArrayList&lt;String&gt; getImagesByBucket(Context mContext , @NonNull String bucketPath){
  
      Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
      String [] projection = {MediaStore.Images.Media.DATA};
      String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" =?";
      String orderBy = MediaStore.Images.Media.DATE_ADDED+" DESC";
  
      ArrayList&lt;String&gt; images = new ArrayList&lt;&gt;();
  
      Cursor cursor = mContext.getContentResolver().query(uri, projection, selection,new String[]{bucketPath}, orderBy);
  
      if(cursor != null){
          File file;
          while (cursor.moveToNext()){
              String path = cursor.getString(cursor.getColumnIndex(projection[0]));
              file = new File(path);
              if (file.exists() &amp;&amp; !images.contains(path)) {
                  images.add(path);
              }
          }
          cursor.close();
      }
      return images;
  }
  </code></pre>
  
  <p>&lt;  ---------- Here is adapter and method to select directory ---------></p>
  
  <pre><code>        &lt;--------------------Adapter------------------&gt;
  
  
  public class GridImageAdapter extends ArrayAdapter&lt;String&gt; {
      private Context mContext;
      private LayoutInflater mInflater;
      private int layoutResource;
      private String mAppend;
      private ArrayList&lt;String&gt; imgURLs;
  
      public GridImageAdapter(Context context, int layoutResource, String append, ArrayList&lt;String&gt; imgURLs) {
          super(context, layoutResource, imgURLs);
          mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          mContext = context;
          this.layoutResource = layoutResource;
          mAppend = append;
          this.imgURLs = imgURLs;
      }
  
      @Override
      public int getCount() {
          return imgURLs.size();
      }
  
      @NonNull
      @Override
      public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
          final ViewHolder holder;
          if (convertView == null) {
              convertView = mInflater.inflate(layoutResource, parent, false);
              holder = new ViewHolder();
              holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.gridImageProgressbar);
              holder.image = (SquareImageView) convertView.findViewById(R.id.gridImageView);
  
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
          final String imgURL = getItem(position);
  
          Picasso.with(mContext).load(mAppend + imgURL).error(R.drawable.ic_person).into(holder.image, new com.squareup.picasso.Callback() {
              @Override
              public void onSuccess() {
                  if (holder.mProgressBar != null) {
                      Picasso.with(mContext).load(mAppend + imgURL).into(holder.image);
                      holder.mProgressBar.setVisibility(View.GONE);
                  }
              }
  
              @Override
              public void onError() {
                  if (holder.mProgressBar != null) {
                      Picasso.with(mContext).load(mAppend + imgURL).into(holder.image);
                      holder.mProgressBar.setVisibility(View.GONE);
                  }
  
              }
          });
  
          return convertView;
      }
  
      private static class ViewHolder {
          SquareImageView image;
          ProgressBar mProgressBar;
      }
  }
  </code></pre>
  
  <p>&lt; -------------- Method to set grid view -----------></p>
  
  <pre><code>private void Directory() {
  
      directories = FileSearch.getImageBuckets(getActivity());
  
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity()
              ,android.R.layout.simple_spinner_item ,directories);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      directorySpinner.setAdapter(adapter);
  
      directorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int position, long id) {
              Log.d(TAG, "onItemSelected: selected" + directories.get(position));
  
              setGridView(directories.get(position));
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {
  
          }
      });
  }
  
  private void setGridView(String selectedDir){
      Log.d(TAG, "setGridView: Dir chosen");
      final ArrayList&lt;String&gt; imageURL = FileSearch.getImagesByBucket(getActivity() ,selectedDir);
  
      int gridWidth = getResources().getDisplayMetrics().widthPixels;
      int imageWidth = gridWidth/NUM_GRID_COLOUMN;
      gridView.setColumnWidth(imageWidth);
  
      GridImageAdapter adapter = new GridImageAdapter(getActivity() , R.layout.grid_image_layout ,append , imageURL);
      gridView.setAdapter(adapter);
  }
  </code></pre>