Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image loading issues #905

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=6.1.1-SNAPSHOT
VERSION_NAME=6.1.2-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void refreshProfileImage(String clientBaseEntityId, ImageView profileImag

}
profileImageView.setTag(org.smartregister.R.id.entity_id, clientBaseEntityId);
DrishtiApplication.getCachedImageLoaderInstance().getImageByClientId(clientBaseEntityId, OpenSRPImageLoader.getStaticImageListener(profileImageView, 0, 0));
DrishtiApplication.getCachedImageLoaderInstance().getImageByClientId(clientBaseEntityId, OpenSRPImageLoader.getStaticImageListener(profileImageView, defaultProfileImage, defaultProfileImage));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
Expand Down Expand Up @@ -68,17 +71,15 @@ public OpenSRPImageLoader(Service service, int defaultPlaceHolderResId) {
mResources = service.getResources();

mPlaceHolderDrawables = new ArrayList<>(1);
mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null
: mResources.getDrawable(defaultPlaceHolderResId));
mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null : mResources.getDrawable(defaultPlaceHolderResId));
}

public OpenSRPImageLoader(Context context, int defaultPlaceHolderResId) {
contextWeakReference = new WeakReference<>(context);
mResources = DrishtiApplication.getInstance().getResources();

mPlaceHolderDrawables = new ArrayList<>(1);
mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null
: mResources.getDrawable(defaultPlaceHolderResId));
mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null : mResources.getDrawable(defaultPlaceHolderResId));
}

/**
Expand All @@ -89,8 +90,7 @@ public OpenSRPImageLoader(FragmentActivity activity, int defaultPlaceHolderResId
this(activity);

mPlaceHolderDrawables = new ArrayList<>(1);
mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null
: mResources.getDrawable(defaultPlaceHolderResId));
mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null : mResources.getDrawable(defaultPlaceHolderResId));
}

/**
Expand Down Expand Up @@ -140,20 +140,15 @@ public static void saveStaticImageToDisk(String entityId, Bitmap image) {
try {

if (entityId != null && !entityId.isEmpty()) {
final String absoluteFileName =
DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG";
final String absoluteFileName = DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG";

File outputFile = new File(absoluteFileName);
os = new FileOutputStream(outputFile);
CompressFormat compressFormat = OpenSRPImageLoader
.getCompressFormat(absoluteFileName);
CompressFormat compressFormat = OpenSRPImageLoader.getCompressFormat(absoluteFileName);
if (compressFormat != null) {
image.compress(compressFormat, 100, os);
} else {
throw new IllegalArgumentException(
"Failed to save static image, could " + "not"
+ " retrieve image compression format from name "
+ absoluteFileName);
throw new IllegalArgumentException("Failed to save static image, could " + "not" + " retrieve image compression format from name " + absoluteFileName);
}
// insert into the db
ProfileImage profileImage = new ProfileImage();
Expand All @@ -173,8 +168,7 @@ public static void saveStaticImageToDisk(String entityId, Bitmap image) {
try {
os.close();
} catch (IOException e) {
Timber.e("Failed to close static images output stream after attempting"
+ " to write image");
Timber.e("Failed to close static images output stream after attempting" + " to write image");
}
}
}
Expand All @@ -194,8 +188,7 @@ public static boolean moveSyncedImageAndSaveProfilePic(@NonNull String syncStatu
boolean successful = false;

if (!entityId.isEmpty()) {
final String absoluteFileName =
DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG";
final String absoluteFileName = DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG";
if (copyFile(imageFile, new File(absoluteFileName))) {

// insert into the db
Expand All @@ -205,8 +198,7 @@ public static boolean moveSyncedImageAndSaveProfilePic(@NonNull String syncStatu
profileImage.setFilepath(absoluteFileName);
profileImage.setFilecategory("profilepic");
profileImage.setSyncStatus(syncStatus);
ImageRepository imageRepo = CoreLibrary.getInstance().context().
imageRepository();
ImageRepository imageRepo = CoreLibrary.getInstance().context().imageRepository();
imageRepo.add(profileImage);

successful = true;
Expand Down Expand Up @@ -276,8 +268,7 @@ public void getImageByClientId(String entityId, OpenSRPImageListener opensrpImag
try {
final Context context = contextWeakReference.get();
if (context != null) {
if (CoreLibrary.getInstance().context().getAppProperties().isTrue(AllConstants.PROPERTY.DISABLE_PROFILE_IMAGES_FEATURE)
|| (entityId == null || entityId.isEmpty())) {
if (CoreLibrary.getInstance().context().getAppProperties().isTrue(AllConstants.PROPERTY.DISABLE_PROFILE_IMAGES_FEATURE) || (entityId == null || entityId.isEmpty())) {

Glide.with(context).load(opensrpImageListener.getDefaultImageResId()).into(opensrpImageListener.getImageView());

Expand All @@ -292,39 +283,41 @@ public void getImageByClientId(String entityId, OpenSRPImageListener opensrpImag

if (imageRecord != null) {

Glide.with(context).load(imageRecord.getFilepath())
.apply(new RequestOptions()
.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)) //Images already on device so skip caching
.transition(DrawableTransitionOptions.withCrossFade(mFadeInImage ? AllConstants.IMAGE_ANIMATION_FADE_IN_TIME : 0))
.placeholder(opensrpImageListener.getDefaultImageResId())
.error(opensrpImageListener.getErrorImageResId())
Glide.with(context).load(imageRecord.getFilepath()).apply(new RequestOptions()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.placeholder(opensrpImageListener.getDefaultImageResId())
.error(opensrpImageListener.getErrorImageResId())) //Images already on device so skip caching
.into(opensrpImageListener.getImageView());


} else {

String url = FileUtilities.getImageUrl(entityId);

AccountAuthenticatorXml authenticatorXml = CoreLibrary.getInstance().getAccountAuthenticatorXml();
String accessToken = AccountHelper.getCachedOAuthToken(CoreLibrary.getInstance().context().allSharedPreferences().fetchRegisteredANM(),
authenticatorXml.getAccountType(),
AccountHelper.TOKEN_TYPE.PROVIDER);

GlideUrl glideUrl = new GlideUrl(url,
new LazyHeaders.Builder()
.addHeader(AllConstants.HTTP_REQUEST_HEADERS.AUTHORIZATION, new StringBuilder(AllConstants.HTTP_REQUEST_AUTH_TOKEN_TYPE.BEARER + " ")
.append(accessToken).toString()).build());

Glide.with(context).load(glideUrl)
.transition(DrawableTransitionOptions.withCrossFade(mFadeInImage ? AllConstants.IMAGE_ANIMATION_FADE_IN_TIME : 0))
.placeholder(opensrpImageListener.getDefaultImageResId())
.error(opensrpImageListener.getErrorImageResId())
.into(opensrpImageListener.getImageView());
String accessToken = AccountHelper.getCachedOAuthToken(CoreLibrary.getInstance().context().allSharedPreferences().fetchRegisteredANM(), authenticatorXml.getAccountType(), AccountHelper.TOKEN_TYPE.PROVIDER);

GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
.addHeader(AllConstants.HTTP_REQUEST_HEADERS.AUTHORIZATION, new StringBuilder(AllConstants.HTTP_REQUEST_AUTH_TOKEN_TYPE.BEARER + " ")
.append(accessToken).toString()).build());
Glide.with(context).load(glideUrl).diskCacheStrategy(DiskCacheStrategy.NONE).placeholder(opensrpImageListener.getDefaultImageResId()).error(opensrpImageListener.getErrorImageResId()).into(new CustomTarget<Drawable>() { // Use CustomViewTarget to load original size image
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
opensrpImageListener.getImageView().setImageDrawable(resource);
if (resource instanceof BitmapDrawable)
saveStaticImageToDisk(entityId, ((BitmapDrawable) resource).getBitmap());
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
opensrpImageListener.getImageView().setImageDrawable(placeholder);
}
});
}
}
}
} catch (Exception e) {
Timber.e(e.getMessage(), e);
Timber.e(e);
}
}

Expand Down
Loading