Android | Image Loading from Exteral storage not working in version 10 or below

I am making a gallery app. I used Glide to load the images. In MainActivity.java I have method named fetchImages() Which is responsible for loading all the urls of images and adding to an arrayList. And then that arrayList is accessed by Glide and it will load the image in RecyclerView. The problem is The code is working properly in Android 11 devices. But, It is not working on the Android 10 or below.

Here is the fetchImages() Method.

...
private void fetchImages() {
        Uri uri;
        Cursor cursor;
        int column_index_data;

        uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.MediaColumns.DATA,MediaStore.Images.Media._ID};
        String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
        cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
        column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        
        while (cursor.moveToNext()) {
            String absoluteImagePath = cursor.getString(column_index_data);
            ImageModel ImageModel = new ImageModel();
            ImageModel.setPath(absoluteImagePath);
            arrayList.add(ImageModel);
        }


        Adapter Adapter = new Adapter(getApplicationContext(), arrayList, MainActivity.this);
        recyclerView.setAdapter(Adapter);
        cursor.close();
    }
...

Here is onBindViewHolder() method from adapter.

...
Glide.with(context)
                .load("file://" + arrayList.get(position).getPath())
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(holder.img);
...

At first I had MediaStore.Images.Media.DISPLAY_BUCKET_NAME in projection array. Which was causing the app to crash in android 10 or below devices. Then I replaced it with current line MediaStore.Images.Media.DISPLAY_NAME. Which stopped the crash. But now, It’s not loading Images. Instead It just shows white grids in RecyclerView. But, the same code is running OK in Android 11 devices.

Solutions Tried

  1. I tried replacing Glide.with()... with GlideApp.with()... but, It didn’t work.
  2. I tried several codes from youtube. But, there are not many tutorials for loading images from external storages.
  3. I tried to replace Uri with Environment.getExternalStorageDirextory() but as it returns a File It didn’t work.
  4. Tried replacing while loop code as MediaStore.MediaColumns.DATA is deprecated. But, this step showed white grids in Android 11 devices too.
...
ImageModel ImageModel = new ImageModel();
            long imageId = cursor.getLong(column_index_data);
            String absoluteImagePath = Uri.withAppendedPath(uri, "" + imageId).toString();
            ImageModel.setPath(absoluteImagePath);
            arrayList.add(ImageModel);
...

Screenshot of white grids

enter image description here

Source: Android Questions

LEAVE A COMMENT