In MainActivity, I'm loading some images using glide into recyclerview according to imageview size.
See:
@Override
public void onBindViewHolder(PreviewAdapter.MyViewHolder holder, int position) {
Glide.with(context).load(previewArrayList.get(position).getUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(holder.postImage);
}
XML:
<ImageView
android:id="@+id/post_image"
android:layout_width="match_parent"
android:layout_marginTop="4dp"
android:layout_height="250dp"
android:layout_below="@+id/post_name"
android:scaleType="centerCrop" />
As you can observe, I'm using Glide cache DiskCacheStrategy.AUTOMATIC
also so that next time without Internet Glide can show the images. Now you can read in this post https://medium.com/@multidots/glide-vs-picasso-930eed42b81d that " Glide resizes the image as per the dimension of the ImageView.
"
Now, I want that final size inside SpalshActivity, which Glide will be stored in cache. So that when After SpalshActivity, when user opens MainActivity without Internet Conncetion for the very first time also, then it should load Images.
So how is it possible?
In SpalshActivity, I'm already caching Images, but it is again downloading/resizing in MainActivity for the very first time.
SpalshActivity:
private void preloadImage(String url) {
try {
//File file = Glide.with(this).asFile().load(url).submit().get();
//String path = file.getPath();
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
if (isPostDataLoaded) {
postImagesLoaded++;
if (postImagesLoaded == postImagesCount) {
binding.progressBar.setVisibility(View.GONE);
AlertDialogManager.showAlertDialogMessage(SplashActivity.this, "Error", "Something went wrong, Please try again later", false, "Exit", null, SplashActivity.this, IS_TABLET);
}
} else {
previewImagesLoaded++;
if (previewImagesLoaded == previewImagesCount) {
binding.progressBar.setVisibility(View.GONE);
AlertDialogManager.showAlertDialogMessage(SplashActivity.this, "Error", "Something went wrong, Please try again later", false, "Exit", null, SplashActivity.this, IS_TABLET);
}
}
return true;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (isPostDataLoaded) {
postImagesLoaded++;
if (postImagesLoaded == postImagesCount) {
PostSingleton.getInstance().setPostMap(postMap);
startFreshActivity(PreviewActivity.class);
}
} else {
previewImagesLoaded++;
if (previewImagesLoaded == previewImagesCount) {
PreviewSingleton.getInstance().setPreviewList(previewList);
getPostImageCount();
postPreloadAllImages();
}
}
return true;
}
}).preload();
} catch (Exception e) {
e.printStackTrace();
}
}