SoFunction
Updated on 2025-03-11

Android How to get all pictures in your phone or pictures in a certain directory

Get all pictures in your phone and filter to get pictures in a certain directory. (Commented codes can be grouped by directory)

private void getAllPhotoInfo() {
    new Thread(new Runnable() {
      @Override
      public void run() {
//        List<MediaBean> mediaBeen = new ArrayList<>();
        HashMap<String,List<MediaBean>> allPhotosTemp = new HashMap<>();//All photos        Uri mImageUri = .EXTERNAL_CONTENT_URI;
        String[] projImage = { ._ID
            , 
            ,
            ,.DISPLAY_NAME};
        final Cursor mCursor = getContentResolver().query(mImageUri,
            projImage,
            .MIME_TYPE + "=? or " + .MIME_TYPE + "=?",
            new String[]{"image/jpeg", "image/png"},
            .DATE_MODIFIED+" desc");

        if(mCursor!=null){
          while (()) {
            // Get the path to the image            String path = (());
            int size = (())/1024;
            String displayName = ((.DISPLAY_NAME));
            // Used to display the album initialization interface            if(("/storage/emulated/0/messageBoard/photoImgs")){
              (new MediaBean(path,size,displayName));
            }

// // Get the parent path name of the image//            String dirPath = new File(path).getParentFile().getAbsolutePath();
//            
// //Storage the corresponding relationship//            if ((dirPath)) {
//              List<MediaBean> data = (dirPath);
//              (new MediaBean(path,size,displayName));
////              (TAG,"getAllPhotoInfo "+()+",path="+(0).getPath()+",name="+(0).getDisplayName());
//              continue;
//            } else {
//              List<MediaBean> data = new ArrayList<>();
//              (new MediaBean(path,size,displayName));
//              (dirPath,data);
////              (TAG,"getAllPhotoInfo else "+()+",path="+(0).getPath()+",name="+(0).getDisplayName());
//            }
          }
          ();
        }
        //Update the interface        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            //...
            (TAG,"mediaBeen="+());
            albumAdapter = new AlbumAdapter(,mediaBeen);
            (albumAdapter);
            (new () {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent cropIntent = new Intent(mContext, );
                ("status",status);
                ("takepath",(position).getPath());
                startActivity(cropIntent);
              }
            });
          }
        });
      }
    }).start();
  }

The above android method to obtain all pictures on your phone or pictures in a certain directory is all the content I share with you. I hope you can give you a reference and I hope you can support me more.