0

I am using MediaStore to fetch the absoluteImagePaths and feed it to Glide in adapter class. currently I am using MediaStore.MediaColumns.DATA to get the path. But, Recently google deprecated that api and may not work in near future. So, what is the replacement for this?

...
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);

        
            cursor.moveToFirst();
            while (cursor.moveToNext()) {
                String absoluteImagePath = cursor.getString(column_index_data);
                ImageModel ImageModel = new ImageModel();
                ImageModel.setPath(absoluteImagePath);
                arrayList.add(ImageModel);
            }
...

This code gives the absolute path. and then it is fed to glide through arrayList.

Solutions Tried

  1. I tried using MediaStore.Images.Media._ID but it it didn't work.
  2. I tried following this link MediaStore.MediaColumns.DATA is deprecated, and I want to load images from gallery to my app but, I couldn't find suitable replacement for my specific code. This link is suggesting to use the MediaStoroe.Images.Media._ID with some little bit tweaks. But, this tweaks gives an Uri not the absolute path. And my code needs to generate absoluteImagePath to feed to Glide.
  3. I then tried to get path from Uri following this link Get filename and path from URI from mediastore but, It led me to again MediaStore.MediaColumns.DATA.
  4. I tried the google as well but, It seems I need to change the some other classes too in order to work with MediaStore.Images.Media._ID.

Note :

  1. I have ImageModel Class to get and set path of the Images.
  2. I have Adapter Class with method onBindViewHolder which contains Glide Method
...
Glide.with(context)
                .load("file://" + arrayList.get(position).getPath())
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(holder.img);
...
  • `But, this tweaks gives an Uri` Yes. Use the uri. Remove .setPath() from your model and add .setUri(uri). Then load from uri as Saurabh showed you. – blackapps Jun 12 '21 at 08:05
  • I did that. And it worked. Thanks. But, Now I tried something new and got some problem. https://stackoverflow.com/questions/67949336/android-how-to-make-bitmap-loading-faster please visit this link . – Anirudhdhsinh Jadeja Jun 12 '21 at 13:34

2 Answers2

0

In Kotlin use this:

 val contentUri: Uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id)                                     
 Glide.with(context).load(contentUri).into(holder.img);
Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31
  • Hey, I followed this, converted into java and it worked. But, I tried something new and got some problem. Do you think you can help me? https://stackoverflow.com/questions/67949336/android-how-to-make-bitmap-loading-faster please follow this link to my question. – Anirudhdhsinh Jadeja Jun 12 '21 at 13:36
0

As @SaurabhDhage mentioned... I adjusted the code and came to this working code. This may available everyplace but, I am posting it for future references. May be someone can get help from it.

Inside MainActivity.java

...
while (cursor.moveToNext()) {
                long imageId = cursor.getLong(column_index_data);
                Uri uriImage = Uri.withAppendedPath(uri, "" + imageId);
                ImageModel ImageModel = new ImageModel();
                ImageModel.setUri(uriImage);
                arrayList.add(ImageModel);
}
...

The GlideMethod

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

Added methods in ImageModel class

...
public Uri getUri() {
        return uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }
...

This will now load the uris instead of paths.