In MainActivity, I\'m loading some images using glide into recyclerview according to imageview size.
See:
@Override
public void onBindViewHolder(P
Better to preload all the images with .downloadOnly()
instead of using any target
. Then load images using FileProvider
.
private class CacheImage extends AsyncTask<String,Void,File> {
@Override
protected File doInBackground(String... strings) {
try {
return Glide.with(getContext())
.load(strings[0])
.downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
.get();
} catch (Exception e) {
Log.e(LOG_TAG,e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(File file) {
if(file!=null){
Uri file_uri = FileProvider.getUriForFile(getContext(),
getContext().getPackageName()+".images",file);
}
}
}
And store the path alongside URL in SQLite.
Now get the image_url
using FileProvider
from SQLite
Glide.with(imageView.getContext())
.load(<image_url>)
.asBitmap()
.dontAnimate()
.centerCrop()
.override(<width>,<height>)
.priority(Priority.IMMEDIATE)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.skipMemoryCache(true)
.into(imageView);
You may also need to add,
In manifest, inside <application>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="{app package name}.images"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Inside res/xml
, as file_paths.xml
,
<paths xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<cache-path name="images" path="image_manager_disk_cache"
tools:path="DiskCache.Factory.DEFAULT_DISK_CACHE_DIR" />
</paths>