I am trying to download blog features images form a WordPress blog page and then load them later.
Right now, every time the user scrolls down and up the image load again and again. Any idea...plzzz..I tried many solutions, for example, this one: Preload multiple images with Glide
But, it didn't work.
I am using implementation 'com.github.bumptech.glide:glide:3.7.0'
And this is my code:
RecyclerViewAdapter
public void getthumbnail(String imageurl, final ImageView imageView, final int position){
ServiceWrapper serviceWrapper = new ServiceWrapper(null);
Call<GetThumbnail> call = serviceWrapper.getThumbnailCall(imageurl);
call.enqueue(new Callback<GetThumbnail>() {
@Override
public void onResponse(Call<GetThumbnail> call, Response<GetThumbnail> response) {
if (response.body() != null && response.isSuccessful()) {
try {
if (response.body().getMediaDetails()!=null){
// Log.e("recycler adapter", " image is here-- " + response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl());
// Log.e("Full IMG SIZE - ", " THIS IS FULL IMAGE URL-- " + response.body().getMediaDetails().getSizes().getFull().getSourceUrl());
imagepath.add(position, response.body().getMediaDetails().getSizes().getFull().getSourceUrl());
Glide.with(mContext)
.load(response.body().getMediaDetails().getSizes().getFull().getSourceUrl())
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
}else {
}
}catch (Exception e){
// Log.e("adapter", "fail not media tag "+ e.toString());
}
}
}
@Override
public void onFailure(Call<GetThumbnail> call, Throwable t) {
// Log.e("adapter", " faile image "+t.toString());
}
});
}
UPDATE I found this code and I put it on top of the other Glide code but it didn't work:
Glide.with(mContext)
.load(response.body().getMediaDetails().getSizes().getFull().getSourceUrl())
.downloadOnly(new SimpleTarget<File>() {
@Override
public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
}
});
UPDATE: Now I used this code and it doesnt download the images faster:
Glide.with(mContext)
.load(response.body().getMediaDetails().getSizes().getFull().getSourceUrl())
.downloadOnly(new SimpleTarget<File>() {
@Override
public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
Glide.with(mContext)
.load(response.body().getMediaDetails().getSizes().getFull().getSourceUrl())
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
}
});
Here is the complete ViewAdapter.java
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<Model> dataset;
private Context mContext;
private ArrayList<Model> list;
private RecyclerViewAdapter adapter;
private RecyclerView recyclerView;
private LinearLayoutManager mLayoutManager;
public static List<WPPost> mListPost;
ArrayList<String> imagepath = new ArrayList<>();
private String baseURL = "https://www.myfitbytes.com/";
public RecyclerViewAdapter(ArrayList<Model> mlist, Context context) {
this.dataset = mlist;
this.mContext = context;
}
public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
TextView title, subtitle, date;
ImageView imageView;
CardView cardview;
public ImageTypeViewHolder(View itemView) {
super(itemView);
this.title = (TextView) itemView.findViewById(R.id.title);
//this.subtitle = (TextView) itemView.findViewById(R.id.subtitle);
this.date = (TextView) itemView.findViewById(R.id.date);
this.imageView = (ImageView) itemView.findViewById(R.id.Icon);
this.cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from( parent.getContext()).inflate(R.layout.postdetails, parent, false);
return new ImageTypeViewHolder(view) ;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final Model object = dataset.get(position);
// Log.d("RecyclerViewAdapter", "IMAGE="+object.Image);
imagepath.add(position, "");
if (Build.VERSION.SDK_INT >= 24)
{
//( (ImageTypeViewHolder) holder).subtitle.setText(Html.fromHtml(object.subtitle , Html.FROM_HTML_MODE_LEGACY));
( (ImageTypeViewHolder) holder).title.setText( Html.fromHtml(object.title , Html.FROM_HTML_MODE_LEGACY) );
( (ImageTypeViewHolder) holder).date.setText( Html.fromHtml(object.date , Html.FROM_HTML_MODE_LEGACY) );
}
else
{
//( (ImageTypeViewHolder) holder).subtitle.setText(Html.fromHtml(object.subtitle ));
( (ImageTypeViewHolder) holder).title.setText( Html.fromHtml(object.title ));
( (ImageTypeViewHolder) holder).date.setText( Html.fromHtml(object.date ));
}
( (ImageTypeViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, WPPostDetails.class);
intent.putExtra("itemPosition", position);
mContext.startActivity(intent);
}
});
try {
getthumbnail(object.Image, ( (ImageTypeViewHolder) holder).imageView, position);
}catch (Exception e){
// Log.e("adapter ","failed to get image "+e.toString() );
}
/// dataset.get(position)
}
public void getthumbnail(String imageurl, final ImageView imageView, final int position){
ServiceWrapper serviceWrapper = new ServiceWrapper(null);
Call<GetThumbnail> call = serviceWrapper.getThumbnailCall(imageurl);
call.enqueue(new Callback<GetThumbnail>() {
@Override
public void onResponse(Call<GetThumbnail> call, final Response<GetThumbnail> response) {
if (response.body() != null && response.isSuccessful()) {
try {
if (response.body().getMediaDetails()!=null){
// Log.e("recycler adapter", " image is here-- " + response.body().getMediaDetails().getSizes().getThumbnail().getSourceUrl());
// Log.e("Full IMG SIZE - ", " THIS IS FULL IMAGE URL-- " + response.body().getMediaDetails().getSizes().getFull().getSourceUrl());
imagepath.add(position, response.body().getMediaDetails().getSizes().getFull().getSourceUrl());
Glide.with(mContext)
.load(response.body().getMediaDetails().getSizes().getFull().getSourceUrl())
.downloadOnly(new SimpleTarget<File>() {
@Override
public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
Glide.with(mContext)
.load(response.body().getMediaDetails().getSizes().getFull().getSourceUrl())
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
}
});
}else {
}
}catch (Exception e){
// Log.e("adapter", "fail not media tag "+ e.toString());
}
}
}
@Override
public void onFailure(Call<GetThumbnail> call, Throwable t) {
// Log.e("adapter", " faile image "+t.toString());
}
});
}
@Override
public int getItemCount() {
return dataset.size() ;
}
}
UPDATE So, now that I tried to update glide 3 to 4.x I am getting the following in error in build.gradle.
All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.1, 27.1.0, 26.1.0. Examples include com.android.support:support-compat:27.1.1 and com.android.support:animated-vector-drawable:27.1.0 less... (⌘F1) There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).
And the code is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.myfitbytes"
minSdkVersion 14
targetSdkVersion 26
versionCode 3
versionName "3.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
//library for wordpress rest api
implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.0.0-beta2'
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:2.0.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
//implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
implementation 'com.android.volley:volley:1.0.0'
implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
//Firebase
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-messaging:17.3.3'
implementation 'org.apache.httpcomponents:httpcore:4.4.1'
//implementation 'org.apache.httpcomponents:httpclient:4.5.6'
//implementation group: 'org.apache.httpcomponents' , name: 'httpclient- android' , version: '4.3.5.1'
implementation files('libs/google-http-client-1.24.1.jar')
implementation files('libs/httpclient-4.5.3.jar')
//bottom nav
implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
//picasso to download image from url faster
implementation 'com.squareup.picasso:picasso:2.71828'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
And, this line is in red:
implementation 'com.android.support:appcompat-v7:26.1.0'