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
- I tried using
MediaStore.Images.Media._ID
but it it didn't work. - 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 generateabsoluteImagePath
to feed toGlide
. - I then tried to get
path
fromUri
following this link Get filename and path from URI from mediastore but, It led me to againMediaStore.MediaColumns.DATA
. - 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 :
- I have
ImageModel
Class toget
andset
path
of the Images. - I have
Adapter
Class with methodonBindViewHolder
which containsGlide
Method
...
Glide.with(context)
.load("file://" + arrayList.get(position).getPath())
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(holder.img);
...