android method of obtaining all pictures in mobile phone or pictures in a certain directory

  • 2021-08-12 03:44:03
  • OfStack

Get all the pictures in the mobile phone, and filter to get the pictures in a 1 directory. (Commented code 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 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projImage = { MediaStore.Images.Media._ID
            , MediaStore.Images.Media.DATA
            ,MediaStore.Images.Media.SIZE
            ,MediaStore.Images.Media.DISPLAY_NAME};
        final Cursor mCursor = getContentResolver().query(mImageUri,
            projImage,
            MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?",
            new String[]{"image/jpeg", "image/png"},
            MediaStore.Images.Media.DATE_MODIFIED+" desc");

        if(mCursor!=null){
          while (mCursor.moveToNext()) {
            //  Get the path of the picture 
            String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            int size = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE))/1024;
            String displayName = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
            // Used to show the photo album initialization interface 
            if(path.contains("/storage/emulated/0/messageBoard/photoImgs")){
              mediaBeen.add(new MediaBean(path,size,displayName));
            }

//            //  Gets the parent pathname of the picture 
//            String dirPath = new File(path).getParentFile().getAbsolutePath();
//            
//            // Storage correspondence 
//            if (allPhotosTemp.containsKey(dirPath)) {
//              List<MediaBean> data = allPhotosTemp.get(dirPath);
//              data.add(new MediaBean(path,size,displayName));
////              Log.e(TAG,"getAllPhotoInfo "+data.size()+",path="+data.get(0).getPath()+",name="+data.get(0).getDisplayName());
//              continue;
//            } else {
//              List<MediaBean> data = new ArrayList<>();
//              data.add(new MediaBean(path,size,displayName));
//              allPhotosTemp.put(dirPath,data);
////              Log.e(TAG,"getAllPhotoInfo else "+data.size()+",path="+data.get(0).getPath()+",name="+data.get(0).getDisplayName());
//            }
          }
          mCursor.close();
        }
        // Update interface 
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            //...
            Log.e(TAG,"mediaBeen="+mediaBeen.size());
            albumAdapter = new AlbumAdapter(AvaterActivity.this,mediaBeen);
            gvAlbum.setAdapter(albumAdapter);
            gvAlbum.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent cropIntent = new Intent(mContext, CropImgActivity.class);
                cropIntent.putExtra("status",status);
                cropIntent.putExtra("takepath",mediaBeen.get(position).getPath());
                startActivity(cropIntent);
              }
            });
          }
        });
      }
    }).start();
  }

Related articles: